《程序员修炼之道》-读书笔记四-NIO复制文件小例子

第2章 新I/O

2.2 文件I/O的基石:Path 标准案例-复制文件

public class SimpleWrite {
    public static void main(String[] args) {

Path file = Paths.get("D:\\mysql-8.0.11-winx64.zip");
// 读取文件
byte[] bytes = read(file);
// 写入文件
write(bytes);
}

public static byte[] read(Path file) {
byte[] bytes = null;
List<String> lines = null;
try {
// 读取行数据,最好是txt
// lines = Files.readAllLines(file, StandardCharsets.UTF_8);
// 什么都能读
bytes = Files.readAllBytes(file);

} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}

public static void write(byte[] bytes) {
Path target = Paths.get("D:\\test.zip");
try {
Files.write(target, bytes);
// Files.write(target,lines);
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

posted @ 2019-07-09 15:33  草莓罐头  阅读(151)  评论(0编辑  收藏  举报