Example usage for java.io FileOutputStream getChannel

List of usage examples for java.io FileOutputStream getChannel

Introduction

In this page you can find the example usage for java.io FileOutputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file output stream.

Usage

From source file:FileLocking.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("file.txt");
    FileLock fl = fos.getChannel().tryLock();
    if (fl != null) {
        System.out.println("Locked File");
        Thread.sleep(100);/*from   ww w.j a  va 2s  .c  o  m*/
        fl.release();
        System.out.println("Released Lock");
    }
    fos.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    if (args.length < 2) {
        System.err.println("Usage: java MainClass inFile1 inFile2... outFile");
        return;//from w  w w . ja v a2  s. co m
    }

    ByteBuffer[] buffers = new ByteBuffer[args.length - 1];
    for (int i = 0; i < args.length - 1; i++) {
        RandomAccessFile raf = new RandomAccessFile(args[i], "r");
        FileChannel channel = raf.getChannel();
        buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
    }

    FileOutputStream outFile = new FileOutputStream(args[args.length - 1]);
    FileChannel out = outFile.getChannel();
    out.write(buffers);
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    URL u = new URL("http://www.java2s.com");
    String host = u.getHost();/*from  w  w  w.  j a  v a2  s . com*/
    int port = 80;
    String file = "/";

    SocketAddress remote = new InetSocketAddress(host, port);
    SocketChannel channel = SocketChannel.open(remote);
    FileOutputStream out = new FileOutputStream("yourfile.htm");
    FileChannel localFile = out.getChannel();

    String request = "GET " + file + " HTTP/1.1\r\n" + "User-Agent: HTTPGrab\r\n" + "Accept: text/*\r\n"
            + "Connection: close\r\n" + "Host: " + host + "\r\n" + "\r\n";

    ByteBuffer header = ByteBuffer.wrap(request.getBytes("US-ASCII"));
    channel.write(header);

    ByteBuffer buffer = ByteBuffer.allocate(8192);
    while (channel.read(buffer) != -1) {
        buffer.flip();
        localFile.write(buffer);
        buffer.clear();
    }

    localFile.close();
    channel.close();
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocate(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();/*from  w  w w  .  j  a va  2 s.co  m*/
        outc.write(bb);
        bb.clear();
    }
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocateDirect(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();//from   www  . ja v  a 2  s.  c om
        outc.write(bb);
        bb.clear();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    FileInputStream inFile = new FileInputStream(args[0]);
    FileOutputStream outFile = new FileOutputStream(args[1]);

    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();

    inChannel.transferTo(0, inChannel.size(), outChannel);

    inChannel.close();//from  www  . j  a  v a2 s . c  o  m
    outChannel.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "s.txt";
    String destination = "d.txt";

    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(destination);

    FileChannel fci = fis.getChannel();
    FileChannel fco = fos.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(1024);

    while (true) {
        int read = fci.read(buffer);

        if (read == -1)
            break;
        buffer.flip();//w ww.  j  a v  a 2 s.c  o m
        fco.write(buffer);
        buffer.clear();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File fromFile = new File("fromFile.txt");
    File toFile = new File("toFile.txt");
    FileInputStream inFile = new FileInputStream(fromFile);
    FileOutputStream outFile = new FileOutputStream(toFile);
    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();
    int bytesWritten = 0;
    long byteCount = inChannel.size();
    while (bytesWritten < byteCount) {
        bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel);
    }//from w  w  w.j a v a  2s.c  o m
    inFile.close();
    outFile.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    FileInputStream inFile = new FileInputStream(args[0]);
    FileOutputStream outFile = new FileOutputStream(args[1]);

    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();

    for (ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); inChannel.read(buffer) != -1; buffer.clear()) {
        buffer.flip();/* w w w . j  a  v  a 2  s.c  o  m*/
        while (buffer.hasRemaining())
            outChannel.write(buffer);
    }

    inChannel.close();
    outChannel.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    FileInputStream inFile = new FileInputStream(args[0]);
    FileOutputStream outFile = new FileOutputStream(args[1]);

    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();

    FileLock outLock = outChannel.lock();
    FileLock inLock = inChannel.lock(0, inChannel.size(), true);

    inChannel.transferTo(0, inChannel.size(), outChannel);

    outLock.release();/*ww w .  j a va 2s  . c  om*/
    inLock.release();

    inChannel.close();
    outChannel.close();
}