FileOutputStream
ByteBuffer
FileChannel
// 01 写 ///
https://www.bilibili.com/video/BV1jK4y1s7GV?p=13
package com.atguigu.nio;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* Author: tz_wl
* Date: 2020/9/20 9:50
* Content:
*/
public class NioFileChannel01 {
public static void main(String[] args) {
try {
//step1 新建一个通道 指向本地文件 d:/111.txt
FileOutputStream fileOutputStream = new FileOutputStream("D:\\tmp\\netty\\1111.txt");
FileChannel fileChannel = fileOutputStream.getChannel();
//step2 新建一个 byteByffer 将文字赛道byteBuffer里面
String strInput="strInput ttttt";
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put(strInput.getBytes());
//step3 将byteBuffer 写入到 channel
byteBuffer.flip(); //flip 前是put flip后是get
fileChannel.write(byteBuffer);
//step4 扫尾工作
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 02 读 ///
https://www.bilibili.com/video/BV1jK4y1s7GV?p=14
package com.atguigu.nio;
import com.sun.xml.internal.ws.encoding.MtomCodec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* Author: tz_wl
* Date: 2020/9/20 10:48
* Content:
*/
public class NioFileChannel02Read {
public static void main(String[] args) {
try {
File file = new File("D:\\tmp\\netty\\1111.txt"); //111.txt 有文字 2222
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel channel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
channel.read(byteBuffer);
System.out.println(new String(byteBuffer.array()));
//2222222222222222222
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 023 读写 ///
https://www.bilibili.com/video/BV1jK4y1s7GV?p=15
package com.atguigu.nio;
import com.sun.jdi.PathSearchingVirtualMachine;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* Author: tz_wl
* Date: 2020/9/20 11:14
* Content:
*/
public class NioFileChannel03ReadWrite {
public static void main(String[] args) throws FileNotFoundException,IOException {
File fileInput = new File("D:\\tmp\\netty\\input01.txt"); //111.txt 有文字 2222
FileInputStream fileInputStream = new FileInputStream(fileInput);
FileChannel channel = fileInputStream.getChannel();
File fileOutput = new File("D:\\tmp\\netty\\output01.txt"); //111.txt 有文字 2222
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
FileChannel channel2 = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while(true){
//注意每次读要清空一下 byteBuffer ,不然要一直读的,可以注释下面内容测试
byteBuffer.clear();
//step1 读channel 数据到 byteBuffer
int count = channel.read(byteBuffer);
System.out.println("count is "+ count);
if(count==-1){
break;
}
//step2 反转
byteBuffer.flip();
// int limit = byteBuffer.limit();
//step3 写byteBuffer 数据到 channel
channel2.write(byteBuffer);
}
fileInputStream.close();
fileOutputStream.close();
}
}