File 的概述和构造方法
1. File 表示什么?
File对象表示路径,可以是文件夹的,也可以是文件的;
这个路径可以是存在的,也可以是不存在的。
2.绝对路径和相对路径是什么意思?
绝对路径是带盘符的。
相对路径是不带盘符的,默认到当前项目下去找。
3. File 的三种构造方法的作用
①把字符串表示的路径变成File对象
public File(String pathname)
//根据字符串表示的路径,变成File对象
String str = "C:\\Users\\28989\\Desktop\\a.txt";
//仅仅是字符串,不能跟本地文件进行关联
//创建File对象,就不是字符串了,就是一个真实路径
File f1 = new File(str);
//变成File对象,就可以使用里面的方法
②把父级路径和子级路径进行拼接
public File(String parent, String child)
根据父路径名字符串 C:\Users\28989\Desktop
和子路径名字符串创建文件对象 a.txt
String parent = "C:\\Users\\28989\\Desktop";
String child = "a.txt";
File f2 = new File(parent,child);
//自己拼接 代码是死的 路径分割符是不同的 不建议使用
//WINDOWS: \
//Linux: /
File f3 = new File(parent+"\\"+child);
③把父级路径和子级路径进行拼接
public File(File parent, String child)
根据父路径对应的文件对象和子路径名字符串创建文件对象
//把一个File表示的路径和String表示的路径进行拼接
File parent2 = new File("C:\\Users\\28989\\Desktop");
String child2 = "a.txt";
File f4 = new File(parent2,child2);