前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >File 类详解

File 类详解

作者头像
帅飞
发布2019-01-22 16:12:14
6530
发布2019-01-22 16:12:14
举报
文章被收录于专栏:Java知其所以然Java知其所以然

File 类入门

File 类是什么

jdk文档中的解释

文件和目录路径名的抽象表示形式。

我的理解

File 其实可以理解为一个桥梁,它建立起了 java 程序和操作系统(windows或linux等)之间的一个关系,方便我们通过面向对象的思维操作操作系统上的文件。

两个常量

1、路径分隔符 ; 2、名称分隔符 / (windows) \ (linux 等)

代码如下:
代码语言:javascript
复制
public class Demo01 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(File.pathSeparator);
        System.out.println(File.separator);
        //路径表示形式
        String path ="E:\\xp\\test\\2.jpg";
        path="E:"+File.separator+"xp"+File.separator+"test"+File.separator+"2.jpg";
        //推荐方式
        path="E:/xp/test/2.jpg";
    }

}

File 类进阶

相对路径与绝对路径构造 File 对象

1、相对路径 File(String parent, String child) ==>File("E:/xp/test","2.jpg") File(File parent, String child) ==> File(new File("E:/xp/test"),"2.jpg") 2、绝对路径 File(String name);

代码如下:
代码语言:javascript
复制
public class Demo02 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String parentPath ="E:/xp/test";
        String name ="2.jpg";
        //相对路径
        File src =new File(parentPath,name);
        src =new File(new File(parentPath),name);
        //输出
        System.out.println(src.getName());
        System.out.println(src.getPath());
        //绝对路径
        src =new File("E:/xp/test/2.jpg");
        System.out.println(src.getName());
        System.out.println(src.getPath());
        //没有盘符: 以 user.dir 构建(也就是项目的根路径,大家动手运行一下就知道了)
        src =new File("test.txt");
        System.out.println(src.getName());
        System.out.println(src.getPath());
        System.out.println(src.getAbsolutePath());
    }

}
File 类常用方法

1、文件名

getName() 文件名、路径名 getPath() 路径名 getAbsoluteFile() 绝对路径所对应的 File 对象 getAbsolutePath() 绝对路径名 getParent() 父目录 ,相对路径的父目录,可能为 null

2、判断信息

exists() canWrite() canRead() isFile() isDirectory()

3、长度(字节数)

不能读取文件夹的长度

length()

4、创建、删除

createNewFile() 不存在创建新文件,存在返回 false delete() 删除文件 static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间 deleteOnExit() 退出虚拟机删除,常用于删除临时文件

代码如下:
代码语言:javascript
复制
public class Demo03 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        test2();
        try {
            test3();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件操作失败");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //创建、删除文件
    public static void test3() throws IOException, InterruptedException{
        //createNewFile() 不存在时创建新文件
        //String path="E:/xp/test/con"; // con 系统关键字
        String path="E:/xp/test/200.jpg";
        //String path="E:/xp/test/1.jpg";
        File src =new File(path);
        if(!src.exists()){
            boolean flag =src.createNewFile();
            System.out.println(flag?"成功":"失败");
        }

        //删除文件
        boolean flag =src.delete();
        System.out.println(flag?"成功":"失败");

        //static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
        //static createTempFile(前缀3个字节长,后缀默认.temp,目录)
        File temp= File.createTempFile("tes", ".temp",new File("e:/xp/test"));      
        Thread.sleep(10000);
        //退出即删除        
        temp.deleteOnExit();

    }

    //2、判断信息
    //3、长度 length()
    public static void test2(){
        //String path ="2.txt";
        String path="E:/xp/test/200.jpg";
        //String path="E:/xp/test/200.jpg";
        File src =new File(path);
        //是否存在
        System.out.println("文件是否存在:"+src.exists());
        //是否可读 写 canWrite() canRead()
        System.out.println("文件是否可写"+src.canWrite());
        System.out.println("============");
        //isFile()
        //isDirectory()
        if(src.isFile()){
            System.out.println("文件");
        }else if(src.isDirectory()){            
            System.out.println("文件夹");
        }else{
            System.out.println("文件不存在");
        }

        System.out.println("是否为绝对路径"+src.isAbsolute());
        System.out.println("长度为:"+src.length());
    }

    //1、名称
    public static void test1(){
        //File src =new File("E:/xp/test/2.jpg");
        //建立联系
        File src =new File("2.txt");
        //返回名称
        System.out.println(src.getName()); 
        //如果是绝对路径,返回完整路径,否则返回相对路径
        System.out.println(src.getPath()); 
        //返回绝对路径
        System.out.println(src.getAbsolutePath());
        //返回上一级目录,如果是相对路径,可能会返回 null
        System.out.println(src.getParent());
    }

}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-08-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java知其所以然 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • File 类入门
    • File 类是什么
      • 两个常量
        • 代码如下:
        • File 类进阶
          • 相对路径与绝对路径构造 File 对象
            • 代码如下:
              • File 类常用方法
                • 代码如下:
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档