首页 > 其他 > 详细

文件创建、查看、删除

时间:2019-08-04 13:01:19      阅读:59      评论:0      收藏:0      [点我收藏+]

技术分享图片

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);
    }
}

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

文件创建、查看、删除

原文:https://www.cnblogs.com/lev1/p/11297574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!