一、JavaNIO读取文件
1、使用缓冲区,非直接缓冲区读写文件
@Test
public void test3() {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel fisChannel = null;
FileChannel fosChannel = null;
try {
fis = new FileInputStream("1.jpg");
fos = new FileOutputStream("2.jpg");
fisChannel = fis.getChannel();
fosChannel = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
while (fisChannel.read(buf) != -1) {
buf.flip();
fosChannel.write(buf);
buf.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fosChannel != null) {
try {
fosChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fisChannel != null) {
try {
fisChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、使用直接缓冲区读写文件,速度快,不建议使用,占内存
@Test
public void test4() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ,
StandardOpenOption.CREATE);
MappedByteBuffer map = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer map2 = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
byte[] bs = new byte[map.limit()];
map.get(bs);
map2.put(bs);
}
3、分散与读取,将通道中的内容分散到缓冲区中
@Test
public void test6() throws Exception {
RandomAccessFile raf = new RandomAccessFile("1.txt", "rw");
FileChannel channel = raf.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(100);
ByteBuffer allocate2 = ByteBuffer.allocate(1000);
ByteBuffer[] bio = { allocate, allocate2 };
channel.read(bio);
for (ByteBuffer byteBuffer : bio) {
byteBuffer.flip();
}
System.out.println(new String(bio[0].array(), 0, bio[0].limit()));
System.out.println("======================");
System.out.println(new String(bio[0].array(), 0, bio[1].limit()));
RandomAccessFile rsf = new RandomAccessFile("2.txt", "rw");
FileChannel channel2 = rsf.getChannel();
channel2.write(bio);
}
@Test
public void test5() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ,
StandardOpenOption.CREATE);
inChannel.transferTo(0, inChannel.size(), outChannel);
outChannel.transferFrom(inChannel, 0, inChannel.size());
inChannel.close();
outChannel.close();
}
@Test
public void test4() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ,
StandardOpenOption.CREATE);
MappedByteBuffer map = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer map2 = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
byte[] bs = new byte[map.limit()];
map.get(bs);
map2.put(bs);
}
@Test
public void test3() {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel fisChannel = null;
FileChannel fosChannel = null;
try {
fis = new FileInputStream("1.jpg");
fos = new FileOutputStream("2.jpg");
fisChannel = fis.getChannel();
fosChannel = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
while (fisChannel.read(buf) != -1) {
buf.flip();
fosChannel.write(buf);
buf.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
if (fosChannel != null) {
fosChannel.close();
}
if (fisChannel != null) {
fisChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1() {
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
String string = "asd";
buf.put(string.getBytes());
buf.flip();
buf.rewind();
buf.clear();
buf.mark();
buf.reset();
if (buf.hasRemaining()) {
System.out.println(buf.remaining());
}
}