FileChannel write
In this chapter you will learn:
- How to write ByteBuffer to a FileChannel
- Transfers bytes from this channel's file to the given writable byte channel
Write ByteBuffer to a FileChannel
The following code uses write(ByteBuffer buffer)
to write byte array in the ByteBuffer
to FileChannel
.
After filling byte array to the ByteBuffer
, remember to
call rewind the reset the reading position for the ByteBuffer
.
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//j a v a 2 s . com
public class MainClass {
public static void main(String args[]) {
FileOutputStream fileOutputStream;
FileChannel fileChannel;
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);
}
}
}
The following code creates a MappedByteBuffer
and map a
FileChannel
to it. Then
it writes the MappedByteBuffer
to another FileChannel.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
//from j a va 2 s. c om
public class MainClass {
public static void main(String args[]) {
FileInputStream fIn;
FileOutputStream fOut;
FileChannel fIChan, fOChan;
long fSize;
MappedByteBuffer mBuf;
try {
fIn = new FileInputStream(args[0]);
fOut = new FileOutputStream(args[1]);
fIChan = fIn.getChannel();
fOChan = fOut.getChannel();
fSize = fIChan.size();
mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
fOChan.write(mBuf); // this copies the file
fIChan.close();
fIn.close();
fOChan.close();
fOut.close();
} catch (IOException exc) {
System.out.println(exc);
System.exit(1);
} catch (ArrayIndexOutOfBoundsException exc) {
System.out.println("Usage: Copy from to");
System.exit(1);
}
}
}
Transfers bytes from one channel to a writable byte channel
abstract long transferTo(long position, long count, WritableByteChannel target)
transfers bytes from this channel's file to the given writable byte channel.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
/*j av a 2 s . c om*/
public class MainClass {
public static void main(String[] args) throws Exception{
String fromFileName = args[0];
String toFileName = args[1];
FileChannel in = new FileInputStream(fromFileName).getChannel();
FileChannel out = new FileOutputStream(toFileName).getChannel();
in.transferTo(0, (int) in.size(), out);
in.close();
out.close();
}
}
Next chapter...
What you will learn in the next chapter:
- How to use RandomAccessFile
- How to create a RandomAccessFile in read mode
- How to seek position with RandomAccessFile
- How to get file length from RandomAccessFile
- How to read into a byte array
- Read a single byte
- Seek position in a RandomAccessFile
- How to write byte array into RandomAccessFile
- How to write a String to a RandomAccessFile
Home » Java Tutorial » NIO