files.exists() :检测文件路径是否存在
public static void main(String[] args) {
Path path = Paths.get("D:/Test");
Boolean fileboolean = Files.exists(path);
System.out.println(fileboolean);
}
files.createFile():创建文件
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:/baba.txt");
Files.createFile(path);
}
files.createDirectory():创建文件夹
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:\\aaa");
Files.createDirectory(path);
}
files.delete():删除一个文件或目录
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:\\baba.txt");
Files.delete(path);
}
files.copy():拷贝文件
public static void main(String[] args) throws IOException {
Path path1 = Paths.get("D:\\aaa.txt");
Path path2 = Paths.get("D:\\bbb.txt");
Files.copy(path1, path2);
}
files.move():移动文件
public static void main(String[] args) throws IOException {
//将d盘下的aaa.txt移动到c盘下并改名为ccc.txt
Path path1 = Paths.get("D:/aaa.txt");
Path path2 = Paths.get("C:/ccc.txt");
Files.move(path1,path2);
}
files.size():文件大小
public static void main(String[] args) throws IOException {
//查看文件大小
Path path = Paths.get("D:\\bbb.txt");
long size = Files.size(path);
System.out.println(size);
}
files.readAllBytes():读取文件内容
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:\\bbb.txt");
byte[] bytes = Files.readAllBytes(path);
String str = new String(bytes);
System.out.println(str);
}