Java文件操作知识整理

一、File类

File是文件和目录路径名的抽象表示形式,即把文件或目录封装成File类的对象。File 类的实例是不可变的;也就是说,一旦创建,File 对象表示的抽象路径名将永不改变。

二、File类中的静态成员变量

public static void main(String[] args){
    //打印结果为 “;”号(Windows下目录之间默认的分隔符)
    String s1 = File.pathSeparator;
    System.out.println(s1);

    //打印结果为“\”(Windows下目录名称之间默认的分隔符)
    String s2 = File.separator;
    System.out.println(s2);
}

三、File类的构造方法

@Test
public void test_01(){
    //File(String pathname)
    //第一个“\”是转义字符,路径可以指定到文件夹,也可以指定到具体的某个文件
    File file = new File("c:\\1.text");
    //打印结果为“c:\1.text”
    System.out.println(file);
}
@Test
public void test_02(){
    //File(String parent, String child)
    //传字符串类型的父路径,字符串类型的自路径
    File file = new File("c:","Intel");
    //打印结果为“c:\Intel”
    System.out.println(file);
}
@Test
public  void  test_03(){
    //File(File parent, String child)
    //第一个参数传父路径的File对象,第二个参数传字符串类型的子路径
    File file_01 = new File("c:");
    File file_02 = new File(file_01, "1.text");
    //打印结果为“c:\1.text”
    System.out.println(file_02);
}

四、File类中的其它的一些方法

//boolean createNewFile() 当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。文件如果存在,不创建
//返回boolean值
@Test
public void test_01() throws IOException {

    File file = new File("c:\\a.txt");
    boolean b = file.createNewFile();
    System.out.println(b);
}
//boolean mkdir()创建此抽象路径名指定的目录。
@Test
public void test_02() throws IOException {

    File file = new File("c:\\新建文件夹");
    boolean b = file.mkdir();
    System.out.println(b);
}
//boolean mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
@Test
public void test_03() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    boolean b = file.mkdirs();
    System.out.println(b);
}
//boolean delete() 删除此抽象路径名表示的文件或目录(慎重!不走回收站)
@Test
public void test_04() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    boolean b = file.delete();
    System.out.println(b);
}
//String getName() 返回由此抽象路径名表示的文件或目录的名称。
@Test
public void test_05() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    String name = file.getName();
    //打印结果为abc
    System.out.println(name);
}
// String getPath() 将此抽象路径名转换为一个路径名字符串。
@Test
public void test_06() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    String name = file.getPath();
    //打印结果为 c:\新建文件夹\abc
    System.out.println(name);
}
//long length() 返回由此抽象路径名表示的文件的长度(大小,单位:字节)
@Test
public void test_07() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    long length = file.length();
    //打印结果为 0
    System.out.println(length);
}
// File getAbsoluteFile()      String  getAbsolutePath()
@Test
public void test_08() throws IOException {

    File file = new File("src");
   // String path = file.getAbsolutePath();
    File f = file.getAbsoluteFile();
    //打印结果为C:\Users\Administrator\Desktop\Java_Idea\src
    System.out.println(f);
}
//boolean exists() 测试此抽象路径名表示的文件或目录是否存在
@Test
public void test_09() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    boolean b = file.exists();
    System.out.println(b);
}
// boolean isDirectory() 测试此抽象路径名表示的文件是否是一个目录。(目录必须存在,不存在直接返回fasle)
@Test
public void test_10() throws IOException {

    File file = new File("c:\\新建文件夹\\abc");
    boolean b = file.isDirectory();
    System.out.println(b);
}
// boolean isFile() 测试此抽象路径名表示的文件是否是一个标准文件。(文件必须存在,不存在直接返回fasle)
@Test
public void test_11() throws IOException {

    File file = new File("c:\\新建文件夹\\abc\\1.txt");
    boolean b = file.isFile();
    System.out.println(b);
}
//String[] list() 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。(遍历目录下所有文件、文件夹,包括隐藏和受保护的文件)
@Test
public void test_12() throws IOException {

    File file = new File("c:\\新建文件夹");
    String[] list = file.list();
    //打印结果为abc
    for (String s: list) {
        System.out.println(s);
    }
}
// File[] listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。(遍历目录下所有文件、文件夹,包括隐藏和受保护的文件,返回每个文件、文件夹的全路径的文件对象)
@Test
public void test_13() throws IOException {

    File file = new File("c:\\新建文件夹");
    File[] files = file.listFiles();
    //打印结果为 c:\新建文件夹\abc
    for ( File f: files) {
        System.out.println(f);
    }
}
//static File[] listRoots() 列出可用的文件系统根。(系统所有的根目录)
@Test
public void test_14() throws IOException {
    File[] files = File.listRoots();
    for ( File f: files) {
        System.out.println(f);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值