目录
前言:学习JAVA的第十八天(基础)-优快云博客
File
- File对象就表示一个路径,可以是文件的路径、也可以是文件夹的路径
- 这个路径可以是存在的,也允许是不存在的
方法:
方法名 | 说明 |
---|---|
File(String pathname) | 根据文件路径创建文件对象 |
File(String parent,String child) | 根据父路径名字符串和子路径名字符串创建文件对象 |
File(File parent,String chlid) | 根据父路径对应文件对象和子路径名字符串创建文件对象 |
测试类
public static void main(String[] args) {
//根据文件路径创建文件对象
String str = "D:\\FileDemo\\a.txt";
File f1 = new File(str);
System.out.println(f1);//D:\FileDemo\a.txt
//父级路径:D:\FileDemo
//子级路径:a.txt
String parent = "D:\\FileDemo";
String child = "a.txt";
File f2 = new File(parent,child);
System.out.println(f2);//D:\FileDemo\a.txt
//把File表示的路径和String表示的进行拼接
File parent2 = new File("D:\\FileDemo");
String child2 = "a.txt";
File f3 = new File(pare