File类

File类简介:

  • 用来将文件或者文件夹封装成对象。
  • 方便对文件以及文件夹的属性信息进行操作

File类的常见方法:

  1 import java.io.*;
  2 
  3 /*
  4 File类常见方法:
  5 1,创建。
  6     boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。
  7                         和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。
  8 
  9     boolean mkdir():创建文件夹。
 10     boolean mkdirs():创建多级文件夹。
 11 2,删除。
 12     boolean delete():删除失败返回false。如果文件正在被使用,则删除不了返回false。
 13     void deleteOnExit();在程序退出时删除指定文件。
 14 
 15 
 16 3,判断。
 17     boolean exists() :文件是否存在.
 18     isFile():是否是文件
 19     isDirectory();:是否是文件夹
 20     isHidden();:是否是隐藏文件
 21     isAbsolute();//是否是绝对路径
 22 
 23 4,获取信息。
 24     getName():返回文件名称
 25     getPath():返回路径名
 26     getParent():返回父级目录
 27 
 28     getAbsolutePath() 
 29     long lastModified() 
 30     long length() 
 31 
 32 
 33     
 34 
 35 
 36 */
 37 
 38 
 39 class FileDemo 
 40 {
 41     public static void main(String[] args) throws IOException
 42     {
 43         method_5();
 44     }
 45 
 46     public static void method_5()
 47     {
 48         File f1 = new File("c:\Test.java");
 49         File f2 = new File("d:\hahah.java");
 50 
 51         sop("rename:"+f2.renameTo(f1));
 52 
 53     }
 54 
 55     public static void method_4()
 56     {
 57         File f = new File("file.txt");
 58 
 59         sop("path:"+f.getPath());
 60         sop("abspath:"+f.getAbsolutePath());
 61         sop("parent:"+f.getParent());//该方法返回的是绝对路径中的父目录。如果获取的是相对路径,返回null。
 62                                     //如果相对路径中有上一层目录那么该目录就是返回结果。
 63 
 64 
 65 
 66     }
 67     
 68     public static void method_3()throws IOException
 69     {
 70         File f = new File("d:\java1223\day20\file2.txt");
 71 
 72         //f.createNewFile();
 73 
 74         //f.mkdir();
 75 
 76 
 77         //记住在判断文件对象是否是文件或者目录时,必须要先判断该文件对象封装的内容是否存在。
 78         //通过exists判断。
 79         sop("dir:"+f.isDirectory());
 80         sop("file:"+f.isFile());
 81 
 82         sop(f.isAbsolute());
 83     }
 84 
 85 
 86     public static void method_2()
 87     {
 88         File f = new File("file.txt");
 89 
 90         //sop("exists:"+f.exists());
 91 
 92         //sop("execute:"+f.canExecute());
 93 
 94         //创建文件夹
 95         File dir = new File("abc\kkk\a\a\dd\ee\qq\aaa");
 96 
 97         sop("mkdir:"+dir.mkdirs());
 98     }
 99     
100 
101     public static void method_1()throws IOException
102     {
103         File f = new File("file.txt");
104 //        sop("create:"+f.createNewFile());
105         //sop("delete:"+f.delete());
106 
107 
108 
109         
110     }
111 
112 
113 
114 
115 
116 
117 
118     //创建File对象
119     public static void consMethod()
120     {
121         //将a.txt封装成file对象。可以将已有的和为出现的文件或者文件夹封装成对象。
122         File f1 = new File("a.txt");
123 
124         //
125         File f2 = new File("c:\abc","b.txt");
126 
127 
128         File d = new File("c:\abc");
129         File f3 = new File(d,"c.txt");
130 
131         sop("f1:"+f1);
132         sop("f2:"+f2);
133         sop("f3:"+f3);
134 
135         File f4 = new File("c:"+File.separator+"abc"+File.separator+"zzz"+File.separator+"a.txt");
136 
137 
138     }
139 
140     public static void sop(Object obj)
141     {
142         System.out.println(obj);
143     }
144 }
View Code
 1 import java.io.*;
 2 class fileDemo2 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         method_2();
 7     }
 8     //返回程序中可以储存的盘(列出机器中有效的盘符)
 9     public static void method_1()
10     {
11         File [] files = File.listRoots();
12         for (File name : files)
13         {
14             sop(name);
15         }
16     }
17     //返回指定目录下的文件及文件夹名称(包含隐藏文件)
18     public static void method_2()
19     {
20         File f = new File("E:\");
21         String [] names = f.list();//调用list方法的file对象必须是封装了一个目录。该目录还必须存在。
22         for (String name : names)
23         {
24             sop(name);
25         }
26     }
27     //标准作业程序
28     public static void sop(Object obj)
29     {
30         System.out.println(obj);
31     }
32 }
View Code2

寻找指定目录中指定格式的文件:

 1 import java.io.*;
 2 class fileDemo3
 3 {
 4     public static void main(String [] args)
 5     {
 6         method_2();
 7     }
 8     //寻找指定目录中以指定格式结尾的文件。
 9     public static void method_1()
10     {
11         File dir = new File("F:\");//指定目录
12         //用匿名内部类的方式实现list(File dir,String name)方法
13         String [] array = dir.list(new FilenameFilter()
14         {
15             public boolean accept(File dir, String name)
16             {
17                 return name.endsWith(".java");
18             }
19         
20         });
21         //for循环遍历打印
22         for(String name : array)
23         {
24             System.out.println(name);
25         }
26     }
27 
28 }
View Code
原文地址:https://www.cnblogs.com/gzc911/p/4929789.html