读取文件很繁琐,需要用到输入流,自从jdk7中新增了Files类后,很多操作都很简单。
读取一个文件的内容:
try {
byte[] bytes = Files.readAllBytes(Paths.get(new File("F://test.txt").toURI()));
String content = new String(bytes);
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
文件拷贝:
Path from = Paths.get(new File("F://test.txt").toURI());
Path to = Paths.get(new File("F://test2.txt").toURI());
try {
Files.copy(from, to);
} catch (IOException e) {
e.printStackTrace();
}
文件移动:
Path from = Paths.get(new File("F://test2.txt").toURI());
Path to = Paths.get(new File("F://test3.txt").toURI());
try {
Files.move(from, to);
} catch (IOException e) {
e.printStackTrace();
}