File类详解

来源:互联网 发布:arduino 网络模块 编辑:程序博客网 时间:2024/05/23 11:40

File类详解

 ONE Goal ,ONE Passion !
  • 1
  • 2

File类是我们接触的比较多的类,当初学习是真是傻傻分不清啊.今天就总结一下file的一些比较不好区分的地方.

首先:当然就是构造方法

File f = new File("文件路径")File f = new File("parent","child")
  • 1
  • 2
  • 3

1,创建一个文件

 //在工作空间目录下创建a.txt的文件     File f = new File("a.txt");      f.createNewFile();      在G:\路径下创建一个a.txt的文件.如果已经有的话这不会重新创建     File f = new File("G:\\a.txt");      f.createNewFile();    如果路径写成\\a.txt,会在盘符下创建新的文件     File f = new File("\\a.txt");      f.createNewFile();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2,创建一个文件夹

 //在工作空间目录下创建a.txt的文件夹     File f = new File("a");      f.mkdir();      在G:\路径下创建一个a.txt的文件夹.如果已经有的话这不会重新创建     File f = new File("G:\\a");      f.mkdir();     如果路径写成\\a.txt,会在盘符下创建新的文件夹     File f = new File("\\a");      f.mkdir();     在g盘下创建文件夹a,a 下创建一个b文件夹     File f = new File("G:\\a\\b");      f.mkdirs();   //注意mkdirs(),创建多个文件夹
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3,new File 的区别:

 File f = new File("a");//此时f是文件夹    File f = new File("parent","child"); //此时f是文件,parent文件夹下的文件    注意:此时会在盘符根目录下创建文件夹 或文件 d    File f = new File("", "d");    f.createNewFile(); // f.mkdir()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4,list()方法与listFiles()方法区别:

f.list();    返回String[]数组.里面包含了f一级目录下的文件和文件夹名.    注意: 如果f:\\a\\b.那么b不会包含在数组中    f.listFiles()    返回File[]数组.里面包含了f一级目录下的文件和文件夹.    注意: 如果f:\\a\\b.那么b不会包含在数组中
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5,文件名过滤器 FilenameFilter

在f1的文件夹中过滤出后缀名为 "txt"的文件
  • 1
  • 2
String[] s = f1.list(new FilenameFilter() {            /**             * dir 需要被过滤的文件夹 name 需要别被过滤的文  件名 .此名是相对路径             * 如果返回true 则证明是符合条件的文件.会将改文件返回到数组中             */            @Override            public boolean accept(File dir, String name) {                File f = new File(dir, name);                if (f.isDirectory()) {                    return false;                }                if (f.getName().endsWith("txt")) {                    return true;                }                return false;            }        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

6,文件过滤器 FileFilter FilenameFilter

  在f1文件夹中过滤出文件长度大于20M的文件.
  • 1
  • 2
  File[] fs = f1.listFiles(new FileFilter() {            /**             * pathname 表示要被过滤的文件,注意:不是文件名             * 返ture 证明是符合条件的文件             */            @Override            public boolean accept(File pathname) {                if (pathname.length() > 1024 * 1024 * 20) {                    return true;                }                return false;            }        });  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

文件一些常用方法:

        f.delete();  //删除file        f.exists();  //file是否存在        f.getName(); //得到file名字        f.isDirectory();  //file是否是文件夹        f.isFile();    //file是否是文件        f.length();   //file的长度
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

文件路径分为两种:

1,绝对路径  G:\\a.txt      2,相对路径  a.txt.   //相对于工作空间的路径( G:\andirodWorkspace\a.txt)
  • 1
  • 2

好了,文件的基本创建就是这些了.接下来整理I/O流.

原创粉丝点击