文件创建、查看、删除

package cn.io;

import java.io.File;
import java.io.IOException;

//文件操作类:创建文件、查看文件信息、删除文件
public class Demo2 {
    //创建文件
    public void create (File file) {
        if(!file.exists()) {
            try {
                file.createNewFile();
                System.out.println("文件已创建");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //查看文件信息
    public void showInfo(File file) {
        if(file.exists()) {
            if(file.isFile()) {
                System.out.println("该文件名是:"+file.getName());
                System.out.println("该文件的相对路径是:"+file.getPath());
                System.out.println("该文件的绝对路径是:"+file.getAbsolutePath());
                System.out.println("该文件的大小是:"+file.length()+"字节");
            }
            if(file.isDirectory()) {
                System.out.println("此文件是目录");
            }
        }else {
            System.out.println("文件不存在");
        }
    }
    //删除文件
    public void delete(File file) {
        if(file.exists()) {
            file.delete();
            System.out.println("文件已删除");
        }
    }
    
    public static void main(String[] args) {
        Demo2 demo2 = new Demo2();
        //File file = new File("abc.txt");
        File file = new File("F:/javaclasszjl/abc.txt");
        //demo2.create(file);
        demo2.showInfo(file);
        //demo2.delete(file);
    }
}

猜你喜欢

转载自www.cnblogs.com/lev1/p/11297574.html