尚硅谷_Java零基础教程-java入门必备-初学者基从入门到精通全套完整版(宋红康主讲) P577-619
(*)表示仅需了解
文章目录
1. File类的使用
- java.io.File类:文件和文件目录路径的抽象表示形式,与平台无关。
- File能新建、删除、重命名文件和目录,但File不能访问文件内容本身。如果要访问文件内容本身,则需要使用输入/输出流。
- 想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。
- File对象可以作为参数传递给流的构造器。
File类的实例化
// 构造器1
String dir = "D:\\web\\IdeaProjects\\chunzhao\\";
File file = new File("hello.txt");
File file1 = new File(dir+"src\\learn\\others\\io\\hello.txt");
System.out.println(file);//hello.txt
System.out.println(file1);//D:\web\IdeaProjects\chunzhao\src\learn\others\io\hello.txt
//构造器2
File file2 = new File(dir , "src\\learn\\others\\io");
System.out.println(file2);//D:\web\IdeaProjects\chunzhao\src\learn\others\io
//构造器3
File file3 = new File(file2, "hello.txt");
System.out.println(file3);//D:\web\IdeaProjects\chunzhao\src\learn\others\io\hello.txt
*路径分隔符
- 路径中的每级目录之间用一个路径分隔符隔开。
- 路径分隔符和系统有关:
- windows和DOS系统默认使用""
- UNIX和URL默认使用"/"
- Java程序支持跨平台,因此路径分隔符要慎用。
- 为了解决这个隐患,File类提供了一个常量:
public static final String separator
。根据操作系统动态的提供分隔符。例如
new File("d:"+File.separator+"java"+File.separator+"demo1"+File.separator+"hello.txt")
File类的常用方法
- File类的获取功能
- getAbsolutePath
- getPath
- getParent
- getName
- length:文件长度(字节数)
- lastModified:毫秒值。可以用时间相关类转化为显示时间
- list:获取名称
- listFiles:获取文件
System.out.println(file1.getAbsolutePath());
//D:\web\IdeaProjects\chunzhao\world.txt --创建文件之后--> 不变
System.out.println(file1.getPath()); //world.txt --创建文件之后--> 不变
System.out.println(file1.getParent()); //null ---> null
System.out.println(file1.getName()); //world.txt ---> world.txt
System.out.println(file1.length()); //0 ---> 12
System.out.println(file1.lastModified()); //0 ---> 1592209152962
File file = new File(this.dir);
String[] list = file.list();
for (String s:list) {
System.out.println(s);
}
// .idea
// chunzhao.iml
// out
// src
// world.txt
File[] files = file.listFiles();
for (File f:files ) {
System.out.println(f);
}
// D:\web\IdeaProjects\chunzhao\.idea
// D:\web\IdeaProjects\chunzhao\chunzhao.iml
// D:\web\IdeaProjects\chunzhao\out
// D:\web\IdeaProjects\chunzhao\src
// D:\web\IdeaProjects\chunzhao\world.txt
- File类的重命名功能
- renameTo:重命名(可以移动路径)
File file1 = new File("world.txt");
File file2 = new File(dir+"src\\learn\\others\\io\\world.txt");
//移动文件路径:需要file1存在,file2不存在
boolean renameTo = file1.renameTo(file2);
System.out.println(renameTo);
- File类的判断功能
- isDirectory
- isFile
- isHidden
- exists
- canRead
- canWrite
File file2 = new File("world.txt");
File file1 = new File(dir+"src\\learn\\others\\io\\world.txt");
//默认值(如果文件不存在)都为false
System.out.println(file1.isAbsolute());
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println(file1.isHidden());
System.out.println(file1.canRead());
System.out.println(file1.canWrite());
System.out.println(file1.canExecute());
-
File类的创建功能
- createNewFile
- mkdir:如果此文件目录存在或上层目录不存在,不创建。
- mkdirs:多级目录
- 如果没有写盘符,默认在项目路径下
-
File类的删除功能
- delete:不走回收站,要注意目录下是否有文件
@Test
public void test6(){
File file = new File("newFile.txt");
if (file.exists()){
file.delete();
System.out.println("删除文件成功");
return;
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.pri