IO流之文件操作

IO流之文件操作

学习完Javaweb后,顿感自己知识储备渺茫,特此学习io流补充知识

文件

文件是保存数据的地方,文件在程序中是通过流的形式来操作的
创建文件的三种方式
1.直接String出文件路径,new file然后file.createNewFile()
2.创建父文件路径new出父文件,然后String子文件路径,File file = new File(parentfile, childfile),然后file.createNewFile();
3.分别String出父文件路径和子文件路径,然后new file,最后创造file.createNewFile();

package com.wang;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

public class creatfile {
    public static void main(String[] args) {

    }
    //文件第一种创建方式
    @Test
    public void creatfile01()
    {
        String filepath="d:/bews1.txt";
        File file = new File(filepath);
        try {
            file.createNewFile();
            System.out.println("success");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //文件第二中创建方式
    @Test
     public void  creatfile02()
     {
         File parentfile = new File("d:/");
         String childfile="news2.txt";
         File file = new File(parentfile, childfile);
         try {
             file.createNewFile();
             System.out.println("成功");
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
     @Test
     public void creatfile03()
     {
         String parentfile="d:/";
         String childfile="news3.txt";
         File file = new File(parentfile, childfile);
         try {
             file.createNewFile();
             System.out.println("ok");
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
}

在这里可以通过@Test注解直接测试方法,方便快捷,但要引入junit的jar包;

获取文件信息

package com.wang;

import org.junit.jupiter.api.Test;

import java.io.File;

public class filegetinformition {
    public static void main(String[] args) {

    }
    @Test
    public void fileifo()
    {
        File file = new File("d:/news2.txt");
        System.out.println("文件名="+file.getName());
        System.out.println("地址="+file.getPath());
        System.out.println("绝对路径="+file.getAbsolutePath());
        System.out.println("文件大小"+file.length());
        System.out.println("是否存在"+file.exists());
        System.out.println("是否是文件"+file.isFile());

    }
}

删除文件

package com.wang;

import org.junit.jupiter.api.Test;

import java.io.File;

public class filedrop {
public static void main(String[] args) {

}
@Test
public void filedeory()
{
    String filename="d:/news2.txt";
    File file = new File(filename);
    if(file.exists())
    {
        if(file.delete())
        {
            System.out.println("哈哈哈,我删除了");
        }
        else
        {
            System.out.println("哈哈哈,我没有被删除");
        }
    }
    else {
        System.out.println("文件不存在");
    }
}

}

删除目录

在Java编程中,目录被当作文件夹

@Test
    public void filelu()
    {
        String filename="d:/hhh05";
        File file = new File(filename);
        if(file.exists())
        {
            if(file.delete())
            {
                System.out.println("哈哈哈,我删除了");
            }
            else
            {
                System.out.println("哈哈哈,我没有被删除");
            }
        }
        else {
            System.out.println("文件不存在");
        }
    }

创建目录

 @Test
    public void filecreatit()
    {
        String filename="d:/hhh01/a/b/c";
        File file = new File(filename);
        if(file.exists())
        {
            System.out.println("文件存在");
        }
        else {
            if(file.mkdirs())
            {
                System.out.println("创建成功");
            }
            else
            {
                System.out.println("失败");
            }
        }
    }

创建目录用mkdir/mkdirs
如果是一级目录可以用mkdir,多级用mkdirs

posted @   jinnice  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示