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);
        }
    }
}

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年午夜至文件最后修改时刻的毫秒数)

@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());
    }

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+" 删除失败");
        }
    }
}

判断目录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("该目录不存在");
    }
}

判断目录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 + "创建失败");
        }
    }
}
  • 列出目录中的文件
public String[] list();//用字符串形式返回目录下的全部文件

public File[] listFiles();//用File对象形式返回目录下的全部文件

public String[] list(FilenameFilter obj);//字符串形式返回目录下的指定类型的所有文件

public File[] listFiles(FilenameFilter obj);//File对象形式返回目录下的指定类型所有文件

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

//创建目录及文件,然后输出
@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);
    }
}

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗半里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值