java IO流技术 之 File

IO流技术java

  概念:input - output 输入输出流:数组

  输入:将文件读到内存中;jvm

  输出:将文件从内存中写出到其余地方ide

  做用:主要就是解决设备和设备之间的数据传输问题。学习

File :文件类的使用十分重要orm

(一)file的构造方法递归

复制代码
 1     public static void test1(){
 2         
 3         //三种拼接路径方式
 4         //1.直接拼接
 5         File filePath = new File("C:\\123\\aaa");
 6         System.out.println("是否存在文件夹--"+filePath.exists());
 7         
 8         //2.先拼接根路径,在组合子路径
 9         File parentPath = new File("C:\\123\\aaa");
10         File childPath = new File(parentPath,"bb.txt");
11         System.out.println("是否存在文件夹--"+childPath.exists());
12     
13         //3.根路径和子路径使用逗号隔开
14         File filePath2 = new File("C:\\123\\aaa","bb.txt");
15         System.out.println("是否存在文件--"+filePath2.exists());
16                 
17     }
复制代码
 ----------------------------------接口

(二)基本方法内存

复制代码
 1 public static void test2() throws IOException{
 2         
 3         //1,.getAbsolutePath()  获取当的本地路径
 4         File file = new File("");
 5         System.out.println("当前java工程的本地绝对路径:"+file.getAbsolutePath());
 6         System.out.println("是否存在文件夹--"+file.exists());
 7         
 8         
 9         //2,.mkdir() 建立文件夹
10         File dirCreatePath = new File("E:\\abc");
11         System.out.println("文件夹是否建立成功--"+dirCreatePath.mkdir());
12         
13         File dirCreatePaths = new File("E:\\abc\\ddd");
14         System.out.println("文件夹是否建立成功--"+dirCreatePaths.mkdirs());
15         
16         //3,.createNewFile(); 指定路径建立一个文件,
17         File fileCreatePath = new File("E:\\abc\\123.txt");
18         File fileCreatePath = new File("E:\\abc\\456.txt");
19         System.out.println("是否建立成功"+fileCreatePath.createNewFile());
20         
21         //4,oldFileNamePath.renameTo(fileNewNamePath)  重命名
22         //(1)方式一
23         File fileName = new File("E:\\abc\\456.txt");
24         if(fileName.exists()){
25             File fileNewName = new File("E:\\abc\\666.txt");
26             System.out.println("重命名是否成功--"+fileName.renameTo(fileNewName));
27         }else{
28             System.out.println("重命名失败!");
29         }
30         
31         //(2)方式二
32         File oldFile = new File("E:\\abc\\666.txt");
33         System.out.println("重命名是否成功--"+oldFile.renameTo(new File("E:\\abc\\888.txt")));
34     
35         //(3),更换存储文件夹(或者存储盘)
36         File oldFile = new File("E:\\abc\\888.txt");
37         System.out.println("更换存储文件夹是否成功--"+oldFile.renameTo(new File("E:\\abc\\ddd\\888.txt")));
38         
39     
40         //5,(1)delete(); 删除文件或者 空 的文件夹
41         File fileDelete = new File("E:\\abc\\123.txt");
42         System.out.println("是否成功删除--"+fileDelete.delete());
43         
44         //(2)deleteOnExit();  在jvm退出时候删除
45         File fileDeleteOnExit = new File("E:\\abc\\ddd\\888.txt");
46         System.out.println("是否成功删除--"+fileDeleteOnExit.deleteOnExit());
47     
48         for(int i = 0;i<=10000;i++){
49             if(i==10000){ //jvm退出后再执行 deleteOnExit()
50                 System.out.println("jvm 已退出!");
51             }
52         }
53     
54     }
复制代码
 ----------------------------------get

(三)判断方法

复制代码
 1     //判断方法
 2 //    isFile()        判断是否是文件(是,则返回 true)
 3 //    isDirectory()   判断是否是文件夹(是,则返回true)
 4 //    isHidden()      判断是否是隐藏文件或文件夹(是,则返回true)
 5 //    isAbsolute()    判断是否是绝对路径(是,返回true)
 6     
 7     public static void test3(){
 8         File file = new File("E:\\abc\\ggg\\hhh.txt");//文件中
 9         File file2 = new File("E:\\abc\\ggg");
10         if(file2.exists()){
11             System.out.println("是文件?"+file.isFile());
12             System.out.println("2是文件夹?"+file2.isDirectory());
13             System.out.println("2是隐藏文件?"+file2.isHidden());
14             System.out.println("是绝对路径?"+file.isAbsolute());
15             System.out.println("2是绝对路径?"+file2.isAbsolute());
16         }
17     }
复制代码
 ----------------------------------

(四)获取方法

复制代码
 1     //获取方法
 2 //    getName()            获取文件名字(路径最后分隔符后面的名称)
 3 //    getPath()            获取路径
 4 //    length()             获取文件容量大小
 5 //    getParent()          获取文件或文件夹的父类路径
 6 //    getAbsolutePath()    获取绝对路径
 7 //    lastModified()       获取文件最后修改的时间戳(毫秒)
 8     
 9     public static void test4(){
10         File file = new File("E:\\abc\\ggg\\hhh.txt");
11         if(file.exists()){
12             System.out.println("文件名称--"+file.getName());
13             System.out.println("文件路径--"+file.getPath());
14             System.out.println("文件绝对路径--"+file.getAbsolutePath());
15             System.out.println("文件大小--"+file.length());
16             System.out.println("文件的父类目录--"+file.getParent());
17             System.out.println("文件路径最后修改时间(毫秒)--"+file.lastModified());
18             
19             //将毫秒转为日期
20             long time = file.lastModified();
21             Date date = new Date(time);
22             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
23             System.out.println("文件路径最后修改时间(毫秒)--"+dateFormat.format(date));
24             
25         }else {
26             System.out.println("不存在");
27         }
28         
29     }
复制代码
 -----------------------------------

(五)对文件的操做

复制代码  1     //文件夹的操做  2     static int sumByte = 0;  3     public static void test5(){  4           5         //【1】获取盘符,返回数组(不须要new)  6         File[] fileRoots = File.listRoots();  7         for(File fileRoot : fileRoots){  8             System.out.println("本机的磁盘:"+fileRoot);  9         } 10          11         //【2】获取文件夹的全部文件数组 12         //(1)建立路径 13         File filePath = new File("E:\\IT学习大纲"); 14          15         //(2)将路径里的文件及文件夹放到文件类数组中 16         File[] files = filePath.listFiles(); 17          18         System.out.println("IT学习大纲:"); 19         //(3)将文件数组遍历出来 20         for(File file1Child : files){ 21              22             //(4)判断为文件夹则直接打印,若是是文件则打印名字并计算容量大小 23             if(file1Child.isDirectory()){ 24                 System.out.println("\n--"+file1Child.getName()); 25                  26                 //(5)依次嵌套遍历。【也能够使用递归调用的方法来一层一层的遍历出下一级的文件或文件夹】 27                 File[] file2 = file1Child.listFiles(); 28                 for(File file2Child : file2){ 29                     if(file2Child.isDirectory()){ 30                          31                         System.out.println("-- --"+file2Child.getName()); 32                          33                         //(6) 34                         File[] file3 = file2Child.listFiles(); 35                         for(File file3Child : file3){ 36                             if(file3Child.isDirectory()){ 37                                 System.out.println("-- -- --"+file3Child.getName()); 38                                  39                             }else{ 40                                 System.out.println("-- -- -->"+file3Child.getName()+"\t"+file3Child.length()+"bt"); 41                                 sumByte += file3Child.length(); 42                             } 43                              44                         } 45                     }else{ 46                         System.out.println("-- -->"+file2Child.getName()+"\t"+file2Child.length()+"bt"); 47                         sumByte += file2Child.length(); 48                     } 49                      50                 } 51                  52             }else{ 53                 System.out.println("-->"+file1Child.getName()+"\t"+file1Child.length()+"bt"); 54                 sumByte += file1Child.length(); 55             } 56         } 57          58         System.out.println("文件的总容为:"+sumByte); 59          60     } 61      62      63     //【3】查找文件 须要定义一个接口类 64     public static void test6(){ 65         File file = new File("E:\\IT学习大纲"); 66         System.out.println(file.exists()); 67         String[] strings = file.list(new MyFilter()); 68          69         for(String string : strings){ 70             System.out.println(string); 71         } 72     } 73  74     //查找文件的过滤器 75 //    class MyFilter implements FilenameFilter{ 76 // 77 //        @Override 78 //        public boolean accept(File dir, String name) { 79 //            // TODO Auto-generated method stub 80 //             81 //            //设置过滤思路  (这里比较后缀名Java) 82 //            //(1)方法一 83 //            //name = name.substring(name.lastIndexOf(".")); 84 //            //return ".java".equals(name); 85 //     86 //            //(2)方法二 87 //            //return name.contains("java"); 88 //     89 //            //(3)方法三 90 //            return name.endsWith("java"); 91 //        } 92 //    } 93       

相关文章
相关标签/搜索