读写字节流
字节流缓冲
import java.io.*;
public class ByteReadWrite {
public static void main(String[] args) {
// 定义源文件
File file = new File("C:\\Users\\we\\Downloads\\maven-practice.zip");
// 获取文件名
String fileName = file.getName();
// 定义目标文件
String targetPath = "E:\\" + fileName;
// 使用自动关闭流的方式
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetPath))) {
// 读取并写入字节流
int len;
byte[] buf = new byte[100 * 1024];
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bos.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
读写字符流
字符流方式
import java.io.*;
public class StringReadWrite {
public static void main(String[] args) {
// 使用自动关闭流的方式
try (FileReader reader = new FileReader("C:\\Users\\we\\Documents\\Todo.txt");
FileWriter writer = new FileWriter("C:\\Users\\we\\Documents\\Copy.txt")) {
// 读写文件
char[] buf = new char[1024];
while (reader.read(buf) != -1) {
writer.write(new String(buf));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
字符流缓冲,按行读取
import java.io.*;
public class StringReadWriteByLine {
public static void main(String[] args) {
// 使用自动关闭流的方式
try (FileReader fr = new FileReader("C:\\Users\\we\\Documents\\Todo.txt");
FileWriter fw = new FileWriter("C:\\Users\\we\\Documents\\Copy.txt");
BufferedReader reader = new BufferedReader(fr);
BufferedWriter writer = new BufferedWriter(fw)) {
// 读写文件
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
大文件拷贝
FinChannel 直接读写
可以采用 NIO 的 FileChannel 多次重复"取水"的方式进行拷贝
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class BigFileCopy {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("E:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
FileOutputStream fos = new FileOutputStream("C:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
FileChannel inc = fis.getChannel();
FileChannel outc = fos.getChannel()
) {
ByteBuffer buffer = ByteBuffer.allocate(256 * 1024 * 1024);
// 多次重复"取水"的方式
while (inc.read(buffer) != -1) {
buffer.flip();
outc.write(buffer);
buffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
零拷贝方式
直接使用 FileChannel 的 transferFrom() 或 transferTo() 方法,底层实现采用的是零拷贝技术。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class BigFileCopy2 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
FileOutputStream fos = new FileOutputStream("C:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
FileChannel srcChannel = fis.getChannel();
FileChannel destChannel = fos.getChannel();
destChannel.transferFrom(srcChannel, 0, srcChannel.size());
destChannel.close();
srcChannel.close();
fis.close();
fos.close();
}
}