栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Java常用文件操作】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Java常用文件操作】

文件

文件就是保存数据的地方 文件流

文件在程序中是以流的形式来操作的

流:数据在数据源(文件)和程序(内存)之间经历的路径

常用的文件操作

创建文件相关的构造器和方法
    方式1public File(String pathname)
public void create01(){
    String filePath = "D:\Test\news1.txt";
    File file = new File(filePath);//此时文件还在内存,不会真正产生
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("文件创建成功...");
}
    方式2 public File(File parent, String child) //根据父目录文件+子路径构建
public void create02(){
    File file = new File("D:\Test\");
    String fileName = "news2.txt";
    File file1 = new File(file, fileName);
    try {
        file1.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("文件创建成功...");
}
    方式3 public File(String parent, String child) //根据父目录+子路径构建
public void create03(){
    String file = "D:\Test\";
    String fileName = "news3.txt";
    File file1 = new File(file, fileName);
    try {
        file1.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("文件创建成功...");
}

注意:路径写成"D:\Test\news1.txt"和"D:/Test/news1.txt"效果等同

获取文件相关的信息

以下为一些File基本方法的演示

public void info() {
    //先创建文件
    File file = new File("D:\Test\news0.txt");

    //调用相应方法得到对应信息
    //getName()
    System.out.println("文件名字 = " + file.getName());
    //getAbsolutePath()
    System.out.println("绝对路径 = " + file.getAbsolutePath());
    //getParent()
    System.out.println("文件父目录 = " + file.getParent());
    //length()
    System.out.println("文件字节大小 = " + file.length());
    //exists()
    System.out.println("文件是否存在 = " + file.exists());//false
    //isFile()
    System.out.println("是否是个文件 = " + file.isFile());//false
    //isDirectory()
    System.out.println("是否是个目录= " + file.isDirectory());//false

}
目录操作和文件删除
    判断 “D:Testnews1.txt” 是否存在,如果存在就删除
public void m1(){
    String filePath = "D:\Test\news1.txt";
    File file = new File(filePath);
    if (file.exists()){
        if (file.delete()){
            System.out.println(filePath+" was deleted...");
        } else {
            System.out.println(filePath + " was failed to deleted...");
        }
    }else {
        System.out.println("The file does not exist...");
    }
}
    判断 “D:Test01” 是否存在,如果存在就删除
public void m2(){
    String filePath = "D:\Test01";
    File file = new File(filePath);
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (file.exists()){
        if (file.delete()){
            System.out.println(filePath+" was deleted...");
        } else {
            System.out.println(filePath + " was failed to deleted...");
        }
    }else {
        System.out.println("The directory does not exist...");
    }
}
    演示创建一级目录 mkdir()
public void m3(){
        String dirPath = "D:\Test01\";
        File file = new File(dirPath);

        if (file.exists()){
            System.out.println(dirPath+" already exists...");
        }else {
            if (file.mkdir()) {//创建一级目录,使用mkdir
                System.out.println(dirPath+" was created successfully...");
            } else {
                System.out.println("Unable to make this directory...");
            }
        }
    }
    演示创建多级目录 mkdirs()
 public void m4(){
        String dirPath = "D:\Test01\a\b\c\";
        File file = new File(dirPath);

        if (file.exists()){
            System.out.println(dirPath+" already exists...");
        }else {
            if (file.mkdirs()) {//创建多级目录,使用mkdir
                System.out.println(dirPath+" was created successfully...");
            } else {
                System.out.println("Unable to make this directory...");
            }
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721646.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号