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:MainClass.java

public static void main(String[] args) {
    try {/*from w w  w.j  av  a 2  s.  co  m*/
        File aFile = new File("test.txt");

        FileOutputStream outputFile = null;
        outputFile = new FileOutputStream(aFile, true);
        FileChannel outChannel = outputFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(200);

        buf.putInt(10).asCharBuffer().put("www.java2s.com");

        buf.position(10).flip();
        outChannel.write(buf);
        buf.clear();

        outputFile.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

}

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();

    ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
    int bytesRead = 0;
    while (bytesRead >= 0 || buffer.hasRemaining()) {
        if (bytesRead != -1)
            bytesRead = inChannel.read(buffer);
        buffer.flip();// ww w. j a v  a2 s. c om
        outChannel.write(buffer);
        buffer.compact();
    }

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

From source file:ExplicitChannelWrite.java

public static void main(String args[]) {
    FileOutputStream fOut;
    FileChannel fChan;/*from w  w  w .  j  av a  2  s . co  m*/
    ByteBuffer mBuf;

    try {
        fOut = new FileOutputStream("test.txt");
        fChan = fOut.getChannel();
        mBuf = ByteBuffer.allocateDirect(26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));
        mBuf.rewind();
        fChan.write(mBuf);
        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileOutputStream fileOutputStream;
    FileChannel fileChannel;/*from  w w  w .j a v  a 2  s .  c o m*/
    ByteBuffer byteBuffer;

    try {
        fileOutputStream = new FileOutputStream("test.txt");
        fileChannel = fileOutputStream.getChannel();
        byteBuffer = ByteBuffer.allocateDirect(26);

        for (int i = 0; i < 26; i++)
            byteBuffer.put((byte) ('A' + i));
        byteBuffer.rewind();
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String phrase = new String("www.java2s.com\n");

    File aFile = new File("test.txt");
    FileOutputStream outputFile = null;
    outputFile = new FileOutputStream(aFile, true);
    System.out.println("File stream created successfully.");

    FileChannel outChannel = outputFile.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    // Load the data into the buffer
    for (char ch : phrase.toCharArray()) {
        buf.putChar(ch);/* w ww.jav a  2 s  .  c  om*/
    }
    System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());

    outChannel.write(buf);
    outputFile.close();
    System.out.println("Buffer contents written to file.");
}

From source file:com.moscona.dataSpace.debug.BadLocks.java

public static void main(String[] args) {
    try {//from w w  w . ja v a 2 s  . c o  m
        String lockFile = "C:\\Users\\Admin\\projects\\intellitrade\\tmp\\bad.lock";
        FileUtils.touch(new File(lockFile));
        FileOutputStream stream = new FileOutputStream(lockFile);
        //            FileInputStream stream = new FileInputStream(lockFile);
        FileChannel channel = stream.getChannel();
        //            FileLock lock = channel.lock(0,Long.MAX_VALUE, true);
        FileLock lock = channel.lock();
        stream.write(new UndocumentedJava().pid().getBytes());
        stream.flush();
        long start = System.currentTimeMillis();
        //            while (System.currentTimeMillis()-start < 10000) {
        //                Thread.sleep(500);
        //            }
        lock.release();
        stream.close();
        File f = new File(lockFile);
        System.out.println("Before write: " + FileUtils.readFileToString(f));
        FileUtils.writeStringToFile(f, "written after lock released");
        System.out.println("After write: " + FileUtils.readFileToString(f));
    } catch (Exception e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

}

From source file:MainClass.java

public static void main(String[] args) {
    String phrase = new String("text\n");
    String dirname = "C:/test"; // Directory name
    String filename = "byteData.txt";
    File aFile = new File(dirname, filename);
    // Create the file output stream
    FileOutputStream file = null;
    try {/*from www . ja  v a2  s  . c  o  m*/
        file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();
    buf.put(bytes);
    buf.flip();
    try {
        outChannel.write(buf);
        file.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    String phrase = new String("www.java2s.com API \n");
    File aFile = new File("test.txt");

    FileOutputStream file = null;
    try {//  www .java  2  s .c om
        file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();
    buf.put(bytes);
    buf.flip();

    try {
        outChannel.write(buf);
        file.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

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

    byte b[] = { 65, 66, 67, 68, 69 };

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    fos.write(b);// ww  w. j  av a 2 s.c  o  m

    // pushes stream content to the underlying file
    fos.flush();

    // returns file channel associated with this stream
    FileChannel fc = fos.getChannel();

    // returns the number of bytes written
    long pos = fc.position();

    System.out.print(pos);

}

From source file:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.bin");
    FileOutputStream outputFile = null;
    try {//  w ww. j a v a 2s.  c  o  m
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int primesWritten = 0;
    while (primesWritten < primes.length) {
        longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten));
        buf.limit(8 * longBuf.position());
        try {
            file.write(buf);
            primesWritten += longBuf.position();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        longBuf.clear();
        buf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}