java.io.File类

 

 

 

 

 

 若hello.txt不存在则返回false

若两个路径都存在也返回false

 

 renameTo(File newName):重命名
                    file1.renameTo(file2):file1必须存在,file2必须不存在

package IOFile;

import org.testng.annotations.Test;

import java.io.File;

/*
java.io.File类:表示目录或文件;提供了一些用于操作文件和目录的基本方法,如:新建、删除、重命名等
                若需要操作文件的内容,File对象无能为力,若需要操作文件的内容,需要用IO流
                因此通常使用File对象与IO流配合操作文件的内容,可以将File对象作为参数传递给IO流的构造器

 */
public class FileTest {
    /*
    访问文件名:
   getName( )
   getPath():获取相对路径
   getAbsoluteFile()
   getAbsolutePath()
   getParent()
   renameTo(File newName):重命名
                    file1.renameTo(file2):file1必须存在,file2必须不存在
     */
    @Test
    public  void test1(){
        File f1=new File("D:\\IdeaProjects\\JavaSE\\Java.IO\\hello2.txt");
        System.out.println(f1.getName());
        System.out.println(f1.getPath());
        System.out.println(f1.getAbsolutePath());
        System.out.println(f1.getAbsoluteFile());
        System.out.println(f1.getParent());
        System.out.println("--------------------------");
        File f2=new File("./hello.txt");
        System.out.println(f2.getName());
        System.out.println(f2.getPath());
        System.out.println(f2.getAbsolutePath());
        System.out.println(f2.getAbsoluteFile());
        System.out.println(f2.getParent());

    }
}

package IOFile;

import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/*
java.io.File类:表示目录或文件;提供了一些用于操作文件和目录的基本方法,如:新建、删除、重命名等
                若需要操作文件的内容,File对象无能为力,若需要操作文件的内容,需要用IO流
                因此通常使用File对象与IO流配合操作文件的内容,可以将File对象作为参数传递给IO流的构造器

 */
public class FileTest {
    /*
    文件检测
    exists()
    canWrite()
    canRead()
    isFile()
    isDirectory()
     */
    @Test
    public void test2(){
        File f1=new File("./hello2.txt");
        System.out.println(f1.exists());
        System.out.println(f1.canWrite());
        System.out.println(f1.canRead());
        System.out.println(f1.isFile());
        System.out.println(f1.isDirectory());
    }
    /*
    获取常规文件信息
    lastModified()
    length():获取的是文件的字节总数;需要注意的是:目录没有大小,必须将目录中所有文件总数的和计算
     */
    @Test
    public  void test3(){
        File f1=new File("./hello2.txt");
        long millis = f1.lastModified();
        System.out.println(millis);
        Date date=new Date(millis);
        System.out.println(date);
        long length = f1.length();
        System.out.println(length);//获取的是字节总数
    }
    /*
    文件操作相关
    createNewFile():新建文件
    delete():删除文件
     */
    @Test
    public void test4() throws IOException {
        File f1=new File("D:\\IdeaProjects\\JavaSE\\Java.IO\\hello3.txt");
        /*if (!f1.exists()){
            boolean b = f1.createNewFile();
            System.out.println(b);
        }*/
        boolean boo = f1.delete();
        System.out.println(boo);
    }
}

 如果目录里面有文件删除目录返回false,没有则返回true

若删除的是目录,则需要将目录中所有的内容清空

前提是所见目录的上级目录存在

 

mkdirs:不管存不存在,直接创建目录 

 

 

该方法获取的是该目录中所有的文件和目录的列表名(名称) 

 

获取该目录下文件的对象

 

package IOFile;

import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/*
java.io.File类:表示目录或文件;提供了一些用于操作文件和目录的基本方法,如:新建、删除、重命名等
                若需要操作文件的内容,File对象无能为力,若需要操作文件的内容,需要用IO流
                因此通常使用File对象与IO流配合操作文件的内容,可以将File对象作为参数传递给IO流的构造器

 */
public class FileTest {
    /*
    访问文件名:
   getName( )
   getPath():获取相对路径
   getAbsoluteFile()
   getAbsolutePath()
   getParent()
   renameTo(File newName):重命名
                    file1.renameTo(file2):file1必须存在,file2必须不存在
     */
    @Test
    public  void test1(){
        File f1=new File("D:\\IdeaProjects\\JavaSE\\Java.IO\\hello2.txt");
        System.out.println(f1.getName());
        System.out.println(f1.getPath());
        System.out.println(f1.getAbsolutePath());
        System.out.println(f1.getAbsoluteFile());
        System.out.println(f1.getParent());
        System.out.println("--------------------------");
        File f2=new File("./hello.txt");
        System.out.println(f2.getName());
        System.out.println(f2.getPath());
        System.out.println(f2.getAbsolutePath());
        System.out.println(f2.getAbsoluteFile());
        System.out.println(f2.getParent());
    }
    /*
    文件检测
    exists()
    canWrite()
    canRead()
    isFile()
    isDirectory()
     */
    @Test
    public void test2(){
        File f1=new File("./hello2.txt");
        System.out.println(f1.exists());
        System.out.println(f1.canWrite());
        System.out.println(f1.canRead());
        System.out.println(f1.isFile());
        System.out.println(f1.isDirectory());
    }
    /*
    获取常规文件信息
    lastModified()
    length():获取的是文件的字节总数;需要注意的是:目录没有大小,必须将目录中所有文件总数的和计算
     */
    @Test
    public  void test3(){
        File f1=new File("./hello2.txt");
        long millis = f1.lastModified();
        System.out.println(millis);
        Date date=new Date(millis);
        System.out.println(date);
        long length = f1.length();
        System.out.println(length);//获取的是字节总数
    }
    /*
    文件操作相关
    createNewFile():新建文件
    delete():删除
            若删除的是目录,则需要将目录中所有的内容清空
     */
    @Test
    public void test4() throws IOException {
        File f1=new File("D:\\IdeaProjects\\JavaSE\\Java.IO\\hello3.txt");
        /*if (!f1.exists()){
            boolean b = f1.createNewFile();
            System.out.println(b);
        }*/
        boolean boo = f1.delete();
        System.out.println(boo);
    }
    /*
    目录操作相关
mkDir() :新建目录;注意:若上级目录不存在则返回false
mkDirs():新建目录,无论是否存在都可创建
list():获取目录中所有文件是String 类型的列表
listFiles():获取该目录下文件的对象
     */
    @Test
    public void test5(){

    }
}
/*
利用FiLe构造器,new一个目录file
1)在其中创建多个文件和目录
2)编写方法,实现删除file中文件的操作
 */
public static boolean deleteFile(File file){
    if (!file.exists()){
        return true
    }
    if (file.isFile()){
        return file.delete();
    }else {//目录
        File[] files = file.listFiles();
        for (File f :files) {
            deleteFile(f);//递归方法调用
        }
        return file.delete();
    }
}
package IOFile;

import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/*
java.io.File类:表示目录或文件;提供了一些用于操作文件和目录的基本方法,如:新建、删除、重命名等
                若需要操作文件的内容,File对象无能为力,若需要操作文件的内容,需要用IO流
                因此通常使用File对象与IO流配合操作文件的内容,可以将File对象作为参数传递给IO流的构造器

 */
public class FileTest {
    /*
    访问文件名:
   getName( )
   getPath():获取相对路径
   getAbsoluteFile()
   getAbsolutePath()
   getParent()
   renameTo(File newName):重命名
                    file1.renameTo(file2):file1必须存在,file2必须不存在
     */
    @Test
    public  void test1(){
        File f1=new File("D:\\IdeaProjects\\JavaSE\\Java.IO\\hello2.txt");
        System.out.println(f1.getName());
        System.out.println(f1.getPath());
        System.out.println(f1.getAbsolutePath());
        System.out.println(f1.getAbsoluteFile());
        System.out.println(f1.getParent());
        System.out.println("--------------------------");
        File f2=new File("./hello.txt");
        System.out.println(f2.getName());
        System.out.println(f2.getPath());
        System.out.println(f2.getAbsolutePath());
        System.out.println(f2.getAbsoluteFile());
        System.out.println(f2.getParent());
    }
    /*
    文件检测
    exists()
    canWrite()
    canRead()
    isFile()
    isDirectory()
     */
    @Test
    public void test2(){
        File f1=new File("./hello2.txt");
        System.out.println(f1.exists());
        System.out.println(f1.canWrite());
        System.out.println(f1.canRead());
        System.out.println(f1.isFile());
        System.out.println(f1.isDirectory());
    }
    /*
    获取常规文件信息
    lastModified()
    length():获取的是文件的字节总数;需要注意的是:目录没有大小,必须将目录中所有文件总数的和计算
     */
    @Test
    public  void test3(){
        File f1=new File("./hello2.txt");
        long millis = f1.lastModified();
        System.out.println(millis);
        Date date=new Date(millis);
        System.out.println(date);
        long length = f1.length();
        System.out.println(length);//获取的是字节总数
    }
    /*
    文件操作相关
    createNewFile():新建文件
    delete():删除
            若删除的是目录,则需要将目录中所有的内容清空
     */
    @Test
    public void test4() throws IOException {
        File f1=new File("D:\\IdeaProjects\\JavaSE\\Java.IO\\hello3.txt");
        /*if (!f1.exists()){
            boolean b = f1.createNewFile();
            System.out.println(b);
        }*/
        boolean boo = f1.delete();
        System.out.println(boo);
    }
    /*
    目录操作相关
mkDir() :新建目录;注意:若上级目录不存在则返回false
mkDirs():新建目录,无论是否存在都可创建
list():获取目录中所有文件是String 类型的列表
listFiles():获取该目录下文件的对象
     */
    @Test
    public void test5(){

    }
    /*
    利用FiLe构造器,new一个目录file
    1)在其中创建多个文件和目录
    2)编写方法,实现删除file中文件的操作
     */
    public static boolean deleteFile(File file){
        if (!file.exists()){
            return true
        }
        if (file.isFile()){
            return file.delete();
        }else {//目录
            File[] files = file.listFiles();
            for (File f :files) {
                deleteFile(f);//递归方法调用
            }
            return file.delete();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值