1 public class Demo1_File {
2
3 /*
4 * File(String pathname) 根据一个路径得到File对象
5 * File(String parent, String child) 根据一个目录和一个子文件/目录得到File对象
6 * File(File parent, String child) 根据一个父类File对象和一个子类文件/目录得到File对象
7 */
8 public static void main(String[] args) {
9 //demo1();
10 //demo2();
11 //将父级目录封装成一个对象是因为这样可以操作文件类的一些方法
12 String parent = "d:\\test";
13 String child = "test.txt";
14 File file = new File(parent);
15 File file2 = new File(file, child);
16 System.out.println(file2.exists());
17
18 }
19
20 /**
21 * 把父级目录和子级目录分开可以使得灵活性更高,更容易修改对应目录
22 */
23 public static void demo2() {
24 String parent = "d:\\test";
25 String child = "test.txt";
26 File file = new File(parent, child);
27 System.out.println(file.exists());
28 }
29
30 public static void demo1() {
31 File file = new File("d:\\test.txt");
32 System.out.println(file.exists());
33 }
34
35 }
1 public class Demo2_File {
2
3 /*
4 * 创建功能:
5 * public boolean createNewFile() 创建文件,如果文件存在,就不再创建返回false
6 * public boolean mkdir() 创建文件夹,如果存在就不再创建返回false
7 * public boolean mkdirs() 创建多级目录,如果父级目录不存在则会帮忙创建出来
8 * 注意: 如果不加盘符,则会默认为当前项目目录
9 *
10 * 重命名功能:
11 * public boolean renameTo(File dest)把文件重命名为指定的文件路径
12 * public boolean delete() 删除文件或文件夹
13 * 重命名注意事项:
14 * 如果路径名相同则只是改名,如果路径名不同,则是改名并剪切
15 * 删除功能注意事项:
16 * Java中的删除不走回收站;
17 * 要删除一个文件夹时,文件夹里面不能包含文件或文件夹,否则会删除失败
18 *
19 * 判断功能:
20 * public boolean isDirectory() 判断是否是目录
21 * public boolean isFile() 判断是否是文件
22 * public boolean exists() 判断文件是否存在
23 *
24 * public boolean canRead() 判断文件是否可读
25 * public boolean canWrite() 判断是否可写
26 * public boolean isHidden() 判断是否隐藏
27 *
28 * 获取功能:
29 * public String getAbsolutePath() 获取绝对路径
30 * public String getPath() 获取构造方法中传入的路径
31 * public String getName() 获取文件或文件夹名称
32 * public long length() 获取长度,字节数
33 * public long lastModified() 获取最后一次的修改时间,毫秒值
34 * public Sting[] list() 获取指定目录下的所有文件或者文件夹的名称数组(仅仅获取文件名称)
35 * public File[] listFiles() 获取指定目录下的所有文件或文件夹的File数据(带文件绝对路径的)
36 *
37 */
38 /**
39 * @param args
40 * @throws IOException
41 */
42 public static void main(String[] args) throws IOException {
43
44 //demo1();
45 //demo2();
46 //demo3();
47 }
48
49 /**
50 * 判断功能
51 */
52 public static void demo3() {
53 File file = new File("aa");
54 file.setReadable(false); //windows系统中认为所有的文件都是可读的
55 file.setWritable(false); //windows系统中可以设置为不可写
56 }
57
58 /**
59 * 重命名及删除功能
60 */
61 public static void demo2() {
62 File file = new File("ccc");
63 System.out.println(file.renameTo(new File("aaaa")));
64 File file1 = new File("aaaa");
65 System.out.println(file1.renameTo(new File("d:\\ccc"))); //剪切并重命名
66
67 File file2 = new File("aaa.txt");
68 System.out.println(file2.delete());
69 File file3 = new File("aaa");
70 System.out.println(file3.delete()); //删除不成功,因为文件夹aaa里面有文件夹
71 }
72
73 /**
74 * @throws IOException
75 * 创建功能
76 */
77 public static void demo1() throws IOException {
78 File file = new File("ccc");
79 System.out.println(file.createNewFile());
80
81 File file2= new File("aaa.txt"); //这样写也是可以的,目录名也可以有后缀名
82 System.out.println(file2.mkdir());
83
84 File file3 = new File("aaa\\ccc");
85 System.out.println(file3.mkdirs());
86 }
87
88 }