347File获取功能的方法和348判断共嫩的方法

获取功能的方法

public String getabsolutepath():返回此Fle的绝对路径名字符串。

public String getpath():将此Fle转换为路径名字符串。

public String getname():返回由此File表示的文件或目录的名称。

public long1 ength():返回由此FIe表示的文件的长度。

File类获取功能的方法
 

   - public String getAbsolutePath() :返回此File的绝对路径名字符串。
    - public String getPath() :将此File转换为路径名字符串。
    - public String getName()  :返回由此File表示的文件或目录的名称。
    - public long length()  :返回由此File表示的文件的长度。

     
        /*
            public long length()  :返回由此File表示的文件的长度。
            获取的是构造方法指定的文件的大小,以字节为单位
            注意:
                文件夹是没有大小概念的,不能获取文件夹的大小
                如果构造方法中给出的路径不存在,那么length方法返回0
         */
        private static void show04() {
            File f1 = new File("C:\\develop\\a\\1.jpg");
            long l1 = f1.length();
            System.out.println(l1);//780831字节
     
            File f2 = new File("C:\\develop\\a\\2.jpg");
            System.out.println(f2.length());//0
     
            File f3 = new File("C:\\develop\\a");
            System.out.println(f3.length());//0 文件夹没有大小概念的
        }
     
        /*
            public String getName()  :返回由此File表示的文件或目录的名称。
            获取的就是构造方法传递路径的结尾部分(文件/文件夹)
         */
        private static void show03() {
            File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
            String name1 = f1.getName();
            System.out.println(name1);//a.txt
     
            File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
            String name2 = f2.getName();
            System.out.println(name2);//shungyuan
        }
     
        /*
            public String getPath() :将此File转换为路径名字符串。
            获取的构造方法中传递的路径
            toString方法调用的就是getPath方法
            源码:
                public String toString() {
                    return getPath();
                }
         */
        private static void show02() {
            File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
            File f2 = new File("a.txt");
            String path1 = f1.getPath();
            System.out.println(path1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
            String path2 = f2.getPath();
            System.out.println(path2);//a.txt
     
            System.out.println(f1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
            System.out.println(f1.toString());//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
        }
     
        /*
            public String getAbsolutePath() :返回此File的绝对路径名字符串。
            获取的构造方法中传递的路径
            无论路径是绝对的还是相对的,getAbsolutePath方法返回的都是绝对路径
         */
        private static void show01() {
            File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
            String absolutePath1 = f1.getAbsolutePath();
            System.out.println(absolutePath1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
     
            File f2 = new File("a.txt");
            String absolutePath2 = f2.getAbsolutePath();
            System.out.println(absolutePath2);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt
        }

 



File类判断功能的方法

- public boolean exists() :此File表示的文件或目录是否实际存在。
- public boolean isDirectory() :此File表示的是否为目录。
- public boolean isFile() :此File表示的是否为文件。

 

     /*
            public boolean isDirectory() :此File表示的是否为目录。
                用于判断构造方法中给定的路径是否以文件夹结尾
                    是:true
                    否:false
            public boolean isFile() :此File表示的是否为文件。
                用于判断构造方法中给定的路径是否以文件结尾
                    是:true
                    否:false
            注意:
                电脑的硬盘中只有文件/文件夹,两个方法是互斥
                这两个方法使用前提,路径必须是存在的,否则都返回false
         */
        private static void show02() {
            //这里的C:\Users\itcast\IdeaProjects\shungyuan\\shungyuan.iml的路径是存在的。
            File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");
     
            //不存在,就没有必要获取
            if(f1.exists()){
                System.out.println(f1.isDirectory());
                System.out.println(f1.isFile());
            }
     
            File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
            if(f2.exists()){
                System.out.println(f2.isDirectory());//true
                System.out.println(f2.isFile());//false
            }
     
            File f3 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\shungyuan.iml");
            if(f3.exists()){
                System.out.println(f3.isDirectory());//false
                System.out.println(f3.isFile());//true
            }
        }
     
        /*
            public boolean exists() :此File表示的文件或目录是否实际存在。
            用于判断构造方法中的路径是否存在
                存在:true
                不存在:false
         */
        private static void show01() {
            File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
            System.out.println(f1.exists());//true
     
            File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");
            System.out.println(f2.exists());//false
     
            File f3 = new File("shungyuan.iml");//相对路径 C:\Users\itcast\IdeaProjects\shungyuan\shungyuan.iml  
            System.out.println(f3.exists());//true
     
            File f4 = new File("a.txt");
            System.out.println(f4.exists());//false

 



posted @ 2022-07-09 09:31  ja不会va  阅读(37)  评论(0)    收藏  举报