File

File

一.分类

文件与文件夹 
用来读写计算机中的文件和文件夹的类,称之为文件类

二.功能

1.获得文件或文件夹的属性信息 

2.创建.删除.重命名文件等 

附代码

要求: 1.可以按下盘符+冒号(如C:)直接进入该盘根目录

            2.键入目录名,按下回车键进入该目录

           3.进入一个目录后,自动列出该目录下所有目录及文件 

           4.按下..返回上一级目录

           5.按下Q键退出程序

public class FileDemo { 
private static String input; 
private static String path;
public static void main(String[] args) {
    System.out.println("请输入目录(如果是盘符,后面需要加':')");
    Scanner scanner = new Scanner(System.in);
    input = scanner.nextLine();
    while (true) {

        File cFile = new File(input + "\\");
        String[] files = cFile.list();
        for (String s : files) {

            File f = new File(input + "\\" + s);
            if (f.isDirectory() && !f.isHidden()) {
                long time = f.lastModified();
                String mTime = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date(time));
                System.out.println(mTime + "\t" + "<DIR>" + "\t\t" + s);
            }
        }
        for (String s : files) {

            File f = new File(input + "\\" + s);
            if (f.isFile() && !f.isHidden()) {
                long time = f.lastModified();
                String mTime = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date(time));
                System.out.println(mTime + "\t" + f.length() / 1024 + "KB" + "\t" + s);
            }
        }
        path = cFile.getPath();
        input = scanner.nextLine();
        if(input.equals("q")){
            break;
        }

        if (input.endsWith(":")) {

        } else if (input.equals("..")) {
            input = cFile.getParent();
            if(input==null){
                System.out.println(path+"已是根目录");
                input = path;
            }
        }else {
            input = path + "\\" + input;
        }

    }

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值