Java工程师知识树

Java基础-IO流-java.io.File

2021-01-10  本文已影响0人  HughJin

Java工程师知识树 / Java基础


描述

java.io.File:是文件和目录路径名的抽象表示,主要用于文件和目录创建、查找和删除等操作。

java.io.File类专门对文件进行操作的类,只能对文件本身进行操作,不能对文件内容进行操作。

明确绝对路径与相对路径的概念:

绝对路径是指文件在硬盘上真正存在的路径

相对路径相对于自己的目标文件位置程序中使用时,相对路径则表示相对于当前的项目目录的路径

java.io.File内常用字段摘要 字段描述
static char separatorChar 与系统有关的默认名称分隔符。
static String separator 与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
System.out.println("E:\\document\\io"+File.separatorChar+"a.txt");//拼接目录与文件所用
System.out.println("E:\\document\\io"+File.separator+"a.txt");//拼接目录与文件所用
System.out.println("E:\\document\\io\\a.txt");

构造方法

构造方法摘要 构造方法描述 实例
File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。 File parentDir = new File("E:\document\io");//父级File对象也就是文件所在目录对象<br />String child = "a.txt";// 子路径字符串 也就是文件名加后缀
File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。 String path = "E:\document\io\a.txt"; // 文件路径名
File(String parent, String child) 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。 String parent = "E:\document\io";// 通过父路径也就文件所在目录<br />String child = "a.txt";// 子路径字符串 也就是文件名加后缀
File(URI uri) 通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。 URI uri = URI.create("file:/E:/document/io/b.txt");//URI对象

File获取与判断功能的方法

打印测试方法

import java.io.File;
import java.net.URI;
import java.text.DateFormat;
import java.util.Date;

public class FileTest extends File {
    public FileTest(String pathname) {
        super(pathname);
    }

    public FileTest(String parent, String child) {
        super(parent, child);
    }

    public FileTest(File parent, String child) {
        super(parent, child);
    }

    public FileTest(URI uri) {
        super(uri);
    }

    @Override
    public String toString() {
        StringBuffer str = new StringBuffer();
        str.append("-----------"+super.toString()+"---------------");

        // File获取方法
        str.append("\r\n");
        str.append("File的名称-getName:");
        str.append(super.getName());
        str.append("\r\n");
        str.append("File的绝对路径名-getAbsolutePath:");
        str.append(super.getAbsolutePath());
        str.append("\r\n");
        /**
         * getPath () 与 getAbsolutePath () 的区别在于,
         * 前者获取的是构造 File 实例时的路径,后者获取的是 File 实例的绝对路径。
         * 当构造 File 实例的路径也是绝对路径时,二者是一样的。
         */
        str.append("File的路径名字符串-getPath:");
        str.append(super.getPath());
        str.append("\r\n");
        str.append("File的路径名字符串-toString:");
        str.append(super.toString());// toString() 调用 return getPath(); 等效于getPath()
        str.append("\r\n");
        str.append("File的长度-length:");
        str.append(super.length());
        str.append("\r\n");
        str.append("File的最后一次被修改的时间-lastModified:");
        str.append(DateFormat.getInstance().format(new Date(super.lastModified())));
        str.append("\r\n");
        str.append("抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null-getParent:");
        str.append(super.getParent());

        // File判断方法
        str.append("\r\n");
        str.append("File是否存在-exists:");
        str.append(super.exists());
        str.append("\r\n");
        str.append("File是否是一个文件-isFile:");
        str.append(super.isFile());
        str.append("\r\n");
        str.append("File是否是一个隐藏文件-isHidden:");
        str.append(super.isHidden());
        str.append("\r\n");
        str.append("File是否是一个目录-isDirectory:");
        str.append(super.isDirectory());
        str.append("\r\n");
        str.append("应用程序是否可以读取此File-canRead:");
        str.append(super.canRead());
        str.append("\r\n");
        str.append("应用程序是否可以修改此File-canWrite:");
        str.append(super.canWrite());
        return str.toString();
    }
}

测试功能代码

import java.net.URI;

public class TestIO {

    public static void main(String[] args) {

        //存在的文件
        FileTest file1 = new FileTest("E:\\document\\io\\a.txt");
        System.out.println(file1);
        //存在的目录
        FileTest file2 = new FileTest("E:\\document\\io");
        System.out.println(file2);
        //不存在的文件
        FileTest file3 = new FileTest("E:\\document\\io\\c.txt");
        System.out.println(file3);
        //存在的文件通过URI方法创建
        URI uri = URI.create("file:/E:/document/io/b.txt");
        FileTest file4 = new FileTest(uri);
        System.out.println(file4);

    }

}

E:\\document\\io目录下的情况为:

创建与删除方法

// 文件的创建
File createNewFileTest = new File("E:\\document\\io\\testCreateNewFile.txt");
System.out.println("是否存在:"+createNewFileTest.exists()); // false
System.out.println("是否创建:"+createNewFileTest.createNewFile()); // true
System.out.println("是否创建:"+createNewFileTest.createNewFile()); // 以及创建过了所以再使用createNewFile返回false
System.out.println("是否存在:"+createNewFileTest.exists()); // true
// 目录的创建
File mkdirTest= new File("newDirectory");
System.out.println("是否存在:"+mkdirTest.exists());// false
System.out.println("是否创建:"+mkdirTest.mkdir()); // true
System.out.println("是否存在:"+mkdirTest.exists());// true
// 创建多级目录
File mkdirsTest= new File("E:\\document\\io\\newDirectoryOne\\newDirectoryTwo");
System.out.println(mkdirsTest.mkdirs());// true
// 文件的删除
System.out.println(createNewFileTest.delete());// true
// 目录的删除
System.out.println(mkdirsTest.delete());// true

遍历目录下文件列表

测试代码:

File directory = new File("E:\\document\\io");
//获取当前目录下的文件以及文件夹的名称。File的getName()集合
String[] names = directory.list();
for(String name : names){
    System.out.println(name);
}
//获取当前目录下的文件以及文件夹对象
File[] files = directory.listFiles();
for (File file : files) {
    System.out.println(file);
}
打印:
a.txt
b.txt
File.md
newDirectoryOne
testCreateNewFile.txt
E:\document\io\a.txt
E:\document\io\b.txt
E:\document\io\File.md
E:\document\io\newDirectoryOne
E:\document\io\testCreateNewFile.txt
上一篇下一篇

猜你喜欢

热点阅读