zoukankan      html  css  js  c++  java
  • I/O操做总结(二)

    文件的操作

    这一节我们来讨论关于文件自身的操作

    不浪费唾沫了,用代码说话……

    实例1:创建文件对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    import java.io.File;
     
    public class Demo {
        public static void main(String[] args) {
            //创建要操作的文件路径和名称
            //其中,File.separator表示系统相关的分隔符,Linux下为:/  Windows下为:\
            //path在此程序里面代表父目录,不包含子文件
            String path = File.separator + "home" + File.separator + "siu" +
                          File.separator + "work" + File.separator;
             
            //childPath在此程序里面代表子目录,包含子文件
            String childPath = File.separator + "home" + File.separator + "siu" +
                               File.separator + "work" + File.separator + "demo.txt";
             
            //用父目录和子文件分隔的方式构造File对象
            //也可以写成 new File("/home/siu/work","test.txt");
            File f1 = new File(path,"test.txt");
             
            //使用绝对路径来构造File对象
            //也可以写成new File("/home/siu/work/demo.txt");
            File f2 = new File(childPath);
             
            //创建父目录的文件对象
            File d = new File(path);
            //使用已有父目录对象和子文件构建新的File对象
            File f3 = new File(d,"hello.txt");
             
            System.out.println("f1的路径" + f1);
            System.out.println("f2的路径" + f2);
            System.out.println("f3的路径" + f3);
        }
    }

    编译后,显示各个File对象所指向的绝对路径

    实例2:创建和删除文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import java.io.File;
    import java.io.IOException;
     
    public class Demo {
        public static void main(String[] args) {
     
            String Path = File.separator + "home" + File.separator + "siu" +
                               File.separator + "work" + File.separator + "demo.txt";
             
            File f = new File(Path);
             
            try {
                /*因为创建和删除文件涉及到底层操作,所以有可能会引发异常*/
                 
                //如果创建成功则会返回true
                //如果已存在该文件,则创建不成功,返回flase,别以为会覆盖
                System.out.println("创建文件:" + f.createNewFile());
                 
                //删除文件,成功返回true,否则返回flase
                System.out.println("删除文件:" + f.delete());
                 
                //此方法表示在虚拟机退出时删除文件
                //原因在于:程序运行时有可能发生异常造成直接退出
                //清理残余很有必要~!
                f.deleteOnExit();
            catch (IOException e) {
                e.printStackTrace();
            }  
        }
    }

     你看,创建成功,所以返回true,因为已经创建好了,所以删除也能成功

    实例3:文件的判断和测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    import java.io.File;
      
    public class Demo {
        public static void main(String[] args) {
      
            String Path = File.separator + "home" + File.separator + "siu" +
                               File.separator + "work" + File.separator + "Demo.txt";
              
            File f = new File(Path);
          
            //判断文件是否可执行
            System.out.println("f是否可执行:" + f.canExecute());
              
            //判断文件是否存在
            System.out.println("f是否存在:" + f.exists());
              
            //判断文件是否可读
            System.out.println("f是否可读:" + f.canRead());
              
            //判断文件是否可写
            System.out.println("f是否可写:" + f.canWrite());
              
            //判断文件是否为绝对路径名
            System.out.println("f是否绝对路径:" + f.isAbsolute());
             
            //判断文件是否为一个标准文件
            System.out.println("f是否为标准文件:" + f.isFile());
             
            //判断文件是否为一个目录
            System.out.println("f是否为目录:" + f.isDirectory());
              
            //判断文件是否隐藏
            System.out.println("f是否隐藏:" + f.isHidden());  
              
        
    }

    这里使用不同的文件做测试便可,设置文件属性什么的也很简单

    需要注意的是,如果使用isFlie()和isDirectory()进行测试,则先要确定文件对象是否已经创建

    实例4:创建目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.io.File;
     
    public class Demo {
        public static void main(String[] args) {
            String path = File.separator + "home" + File.separator + "siu" +
                          File.separator + "work" + File.separator;
             
            //path在此处作为父目录存在
            File f1 = new File(path,"/abc");
            File f2 = new File(path,"/d/e/f/g");
             
            //创建一个目录
            System.out.println(f1.mkdir());
             
            //递归创建目录
            System.out.println(f2.mkdirs());
             
        }  
    }

     注意看路径

    实例5:获取文件信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import java.io.File;
     
    public class Demo {
        public static void main(String[] args) {
            String path = File.separator + "home" + File.separator + "siu" +
                          File.separator + "work" + File.separator + "demo.txt";
             
            File f = new File(path);
             
            //返回文件的绝对路径
            //此处返回值为String
            System.out.println("f的绝对路径名:" + f.getAbsolutePath());
             
            //返回文件的绝对路径
            //此处返回值为File
            System.out.println("f的绝对路径对象:" + f.getAbsoluteFile());
             
            //返回文件或目录的名称
            System.out.println("f的名称:" + f.getName());
             
            //返回文件的相对路径
            //构造函数中封装的是什么路径,就返回什么路径
            System.out.println("f的路径:" + f.getPath());
             
            //返回父目录的路径
            //如果在构造函数中的路径不是绝对路径,那么此处返回null
            System.out.println("f的父目录:" + f.getParent());
             
        }  
    }

     这些都是比较常用并且功能类似的方法,至于不常用的信息获取参考API即可

    实例6:列出文件系统的根目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import java.io.File;
     
    public class Demo {
        public static void main(String[] args) {
            //listRoots()是一个静态方法,返回文件数组
            File[] files = File.listRoots();
            //foreach循环打印File对象
            for (File x : files) {
                System.out.println(x);
            }
        }
    }

    因为本地环境是Linux,所以根目录只有一个 /,如果是Windows就能列出你的所有盘符

    实例7:列出目录下的所有文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import java.io.File;
     
    public class Demo {
        public static void main(String[] args) {
            String path = File.separator + "opt" + File.separator;
             
            File f = new File(path);
             
            //方式一:list()
            //返回一个包含指定目录下所有文件名的字符串数组
            //如果不是一个目录则返回null
            String[] files = f.list();
            for (String x : files) {
                System.out.println(x);
            }
             
            //方式二:listFiles()
            //返回File数组
            /*
            File[] files = f.listFiles();
            for (File x : files) {
                //如果需要包含路径,则直接打印x即可
                System.out.println(x.getName());
            }
            */
             
        }
    }

     两者都是返回目录下的所有文件名,但是第二种方式更实用,为递归列出文件做铺垫

    实例8:递归列出目录下所有文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.io.File;
     
    public class Demo {
        public static void main(String[] args) {
            String path = File.separator + "opt" + File.separator;
             
            File f = new File(path);
            //调用下面的递归方法
            print(f);
        }
         
        //用递归的方式打印目录列表
        public static void print(File f) {
            if(f.isDirectory()){
                File[] files = f.listFiles();
                for(File x : files) {
                    print(x);
                }
            else {
                System.out.println(f);
            }
        }
    }

    好吧,打印内容太多了,意思意思就行了

  • 相关阅读:
    【故障处理】ORA-12162: TNS:net service name is incorrectly specified (转)
    android studio 编程中用到的快捷键
    java时间格式串
    android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
    linux安装vmware
    x1c 2017 安装mint18的坑——grub2
    x1c2017 8G版 win linux的取舍纠结记录
    python的try finally (还真不简单)
    kafka+docker+python
    json文件不能有注释
  • 原文地址:https://www.cnblogs.com/jmsjh/p/7504552.html
Copyright © 2011-2022 走看看