知识学习-------------java io流

一.Java文件创建

Java文件有三种创建方式

1.1第一种

代码展示:

@Test
    public void create01() throws IOException {
        String filePath = "e:\\news.txt";
        File file = new File(filePath);
        file.createNewFile();

    }

1.2第二种

代码展示:

 @Test
    public void create2() throws IOException {
        File parentFile = new File("e:\\");
        String fileName = "news2.txt";
        File file = new File(parentFile, fileName);
        file.createNewFile();
    }

1.3第三种

代码展示:

 @Test
    public void create3() throws IOException {
        String parentPath = "e:\\";
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);
        file.createNewFile();

二.获取文件信息

将会用到相关方法:

代码展示:

 @Test
    public void fileInfo(){
        File file = new File("e:\\news.txt");
        System.out.println("文件名="+ file.getName());
        System.out.println("文件绝对路径:"+file.getAbsolutePath());
        System.out.println("文件父目录:"+file.getParentFile());
        System.out.println("文件大小:"+file.length());
        System.out.println("文件是否存在:"+file.exists());//True
        System.out.println("是不是一个文件:"+file.isFile());//T
        System.out.println("是不是一个目录:"+file.isDirectory());//F
    }

结果展示:

因为news.txt是我们刚刚创建的,所以大小为0,正确

三.小结

1)Java文件创建比较简单,但之前也只知道第一种创建方式,现在系统的了解了一下,并记录下来

2)创建完文件之后,可以对文件有一系列的操作,比如file.getName得到文件名,file.getAbsolutePath得到文件的绝对路径,file.getParent得到文件的父目录,file.lenth得到文件的大小,file.exists判断文件是否存在,file.isFile判断是不是文件,file.isDirectory判断是不是目录等操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值