• Java文件操作【教你用Java运行微信】


    ➤ Java 输入输出IO流 全部导航

    java

    01 创建文件

    • new File(String pathname) //根据路径创建一个File对象;
    • new File(File parent,String child) //根据父目录文件+子路径创建;
    • new File(String parent,String child) //根据父目录+子路径构建
    • creatNewFile 创建新文件

    示例:用三种不同的方式创建文件 news1.txt 、news2.txt 、news3.txt

    import org.junit.Test;
    
    import java.io.File;
    import java.io.IOException;
    
    public class FileCreate {
        public static void main(String[] args) {}
    
        //方式1 new File(String pathname) 根据路径创建一个File对象
        @Test
        public void create01(){
            String filePath = "d:\\news01.txt";
            File file = new File(filePath);
            //需要捕获IO异常
            try {
                file.createNewFile();
                System.out.println("方式一:文件创建成功! ");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        //方式2 new File(File parent,String child) 根据父目录文件+子路径构建
        @Test
        public void create02(){
            File parentFile = new File("d:\\");
            String fileName = "news02.txt";
            //这里的File对象,在Java中只是一个对象
            //只有执行了creatNewFile()方法,才会真正的在磁盘中创建该文件
            File file = new File(parentFile,fileName);
            //需要捕获IO异常
            try {
                file.createNewFile();
                System.out.println("方式二:文件创建成功~");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        //方式3 new File(String parent,String child) 根据父目录+子路径构建
        @Test
        public void creat03(){
            String parentPath = "d:\\";
            String fileName = "news03.txt";//这里还可以继续写路径,但由于只有一层所以为文件名
            File file = new File(parentPath, fileName);
    
            try {
                file.createNewFile();
                System.out.println("方式三:文件创建成功*");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    02 获取文件信息

    经常使用File类的下列方法获取文件本身的一些信息:

    public String getName() //获取文件名字
    
    public boolean canRead() //判断文件是否可读
    
    public boolean canWrite() //判断文件是否可写
    
    public boolean exits() //判断文件是否存在
    
    public long length() //获取文件的长度(单位是字节)
    
    public String getAbsolutePath() //获取文件的绝对路径
    
    public String getParent() //获取文件的父目录
    
    public boolean isFile() //判断文件是否是一个普通文件,而不是目录
    
    public boolean isDirectory() //判断文件是否是一个目录
    
    public boolean isHidden() //判断文件是否是隐藏文件
    
    public long lastModified() //获取文件最后修改时间(返回的是1970年午夜至文件最后修改时刻的毫秒数)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    @Test注解 可以测试运行方法,不用通过main方法调用
    (需要导入包:import org.junit.Test;

    //获取文件的信息
        @Test
        public void fileInformation(){
            //先创建对象
            File file = new File("d:\\news01.txt");
            //调用相应的方法得到对应信息
            System.out.println("文件名字: "+file.getName());
            System.out.println("文件的绝对路径: "+file.getAbsolutePath());
            System.out.println("文件的父级目录: "+file.getParent());
            System.out.println("文件大小(字节): "+file.length());//先在文件中写入一个HelloWorld
            System.out.println("文件是否存在: "+file.exists());
            System.out.println("是否是一个目录: "+file.isDirectory());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    03 目录操作和文件删除

    • mkdir 创建一级目录
    • mkdirs 创建多级目录
    • delete 删除空目录或文件

    判断d:\news02.txt是否存在,存在就删除

    //判断d:\\news02.txt是否存在,存在就删除
    @Test
    public void m1(){
        String filePath = "d:\\news02.txt";
        File file = new File(filePath);
        if(file.exists()){
            if(file.delete()){
                System.out.println(filePath+" 删除成功");
            }else{
                System.out.println(filePath+" 删除失败");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    判断目录D:\demo是否存在,如果存在就删除,否则提示不存在

    //判断目录D:\\demo是否存在,如果存在就删除,否则提示不存在
    //Java编程中,目录也被当做文件(特殊的文件)
    @Test
    public void m2(){
        String filePath = "D:\\demo00";
        File file = new File(filePath);
        file.mkdir();//创建目录
        if(file.exists()){
            if(file.delete()){
                System.out.println(filePath+"目录删除成功");
            }else{
                System.out.println(filePath+"目录删除失败");
            }
        }else{
            System.out.println("该目录不存在");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    判断目录D:\demo\a\b\c是否存在,如果不存在就创建

    //判断目录D:\\demo\\a\\b\\c是否存在,如果不存在就创建
    @Test
    public void m3(){
        String directoryPath = "D:\\demo\\a\\b\\c";
        File file = new File(directoryPath);
        if(file.exists()){
            System.out.println(directoryPath+"存在");
        }else{
            if (file.mkdirs()) {//多级目录要用mkdirs 一级用mkdir
                System.out.println(directoryPath+"创建成功");
            }else {
                System.out.println(directoryPath + "创建失败");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 列出目录中的文件
    public String[] list();//用字符串形式返回目录下的全部文件
    
    public File[] listFiles();//用File对象形式返回目录下的全部文件
    
    public String[] list(FilenameFilter obj);//字符串形式返回目录下的指定类型的所有文件
    
    public File[] listFiles(FilenameFilter obj);//File对象形式返回目录下的指定类型所有文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    创建目录及文件,然后输出

    //创建目录及文件,然后输出
    @Test
    public void list(){
        String path = "d:\\Test";
        String fileName1 = "d:\\Test\\test01.doc";
        String fileName2 = "d:\\Test\\test02.doc";
        String fileName3 = "d:\\Test\\test03.doc";
        File filepath = new File(path);
        File file1 = new File(fileName1);
        File file2 = new File(fileName2);
        File file3 = new File(fileName3);
        if (filepath.mkdir()) {
            System.out.println("目录创建成功");
        }
        try {
            file1.createNewFile();
            file2.createNewFile();
            file3.createNewFile();
            System.out.println("文件创建成功");
        }catch(IOException e){
            throw new RuntimeException(e);
        }
        for (String str:filepath.list()
             ) {
            System.out.println(str);
        }
    }
    
    • 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

    04 运行可执行文件

            当要执行一个本地机上可执行的文件时,可以使用 java.lang 包中的 Runtime 类

    打开电脑上的微信:

    @Test
        public void run(){
            File file = new File("D:\\WeChat\\WeChat.exe");//WeChat.exe的存储路径
            //1. 先申明对象
            Runtime ec;
            //2. 使用该类的getRuntime()静态方法创建对象
            ec = Runtime.getRuntime();
            //3.调用exec(String command)方法打开一个可执行文件或者执行一个操作
            try {
                ec.exec(file.getAbsolutePath());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    IO流总结
    软件测试流程分享
    使用java调用C语言程序教程
    聊聊运行时监控方案
    [2022-11-13]神经网络与深度学习 hw8 -RNN造轮子篇
    Java入门基础第4天《Java编程规范及编译源代码常见错误的解决方法》
    计算机毕业设计项目选题推荐(免费领源码)Java+springboot+Mysql停车微信小程序小程序92714
    5.第五部分 异步爬虫
    Elastic学习之旅 (2) 快速安装ELK
    看我为了水作业速通 opengl freeglut!
  • 原文地址:https://blog.csdn.net/Lov1_BYS/article/details/127926291