zoukankan      html  css  js  c++  java
  • java(File类、递归)(了解80%~95%)

    第一题:

    通过File输出当前项目目录下的文件"myfile.txt"的名字,大小,最后修改时间。
    最后修改时间格式如:2016-03-23 14:22:16
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    public class Demo05 {
        public static void main(String[] args) {
            File file=new File("E:\桌面\myfile.txt");
            //输出文件的名字
            String name = file.getName();
            System.out.println("项目目录下的名字为:"+name);
            //输出文件的大小
            long l = file.length();
            System.out.println("项目目录下文件的大小为:"+l);
            //输出文件最后修改的时间
            SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //SimpleDateFormat s1=new SimpleDateFormat("2016-03-23 14:22:16");
            //System.out.println("项目目录下的文件最后修改的时间为:"+s1.format(file.lastModified()));
            Calendar cal = Calendar.getInstance();
            long l1 = file.lastModified();
            cal.setTimeInMillis(l1);
            System.out.println(s1.format(cal.getTime()));
    
        }
    }

    第二题:

    要求用户输入一个文件名并使用File在当前目录下创建出来。
    若该文件已经存在,则提示用户该文件已经存在。并创建该文件副本:
    例如:用户输入"test.txt".若该文件已存在,提示用户存在后,创建名为:test_副本1.txt 的文件
    若该文件也存在了,则创建名为:test_副本2.txt 的文件,以此类推
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class Demo0502 {
        public static void main(String[] args) throws IOException {
            //此扫描器执行当前行,并返回跳过的输入信息。nextLine()
            System.out.println("请输入一个文件夹名:");
            String s1=new Scanner(System.in).nextLine();
            File file=new File(s1);
            //System.out.println(file.getName());
            //将此抽象路径名转换为一个路径名字符串。getPath()
            String path = file.getPath();
            if (path.equals("test.txt")) {
                System.out.println("您输入的文件已存在");
                boolean file1 = file.createNewFile();
                while (file1 == true) {
                    file.renameTo(new File("test_副本1"));
                    System.out.println("创建副本1成功");
                    break;
                }
            }else if (path.equals("test_副本1.txt")){
                System.out.println("您输入的文件已存在");
                boolean file2 = file.createNewFile();
                while (file2==true){
                    file.renameTo(new File("test_副本2"));
                    System.out.println("创建副本2成功");
                    break;
                }
            }
        }
    }

    第三题:

    获取并输出当前目录下的所有文件和目录的名字
    import java.io.File;
    public class Demo0504 {
        public static void main(String[] args) {
            File file=new File("G:\my");
            File[] files = file.listFiles();
            for (File f:files) {
                System.out.println("所有文件:"+f.getName());
            }
            System.out.println("目录名字:"+file.getName());
        }
    }

    第四题:

    要求用户输入一个文件或目录名,并删除当前目录下的该文件或目录。
    可自行手动先在当前项目目录中创建一个要删除的文件或目录,若是目录,还可以
    在该目录中继续创建若干级目录和文件。
    import java.io.File;
    import java.util.Scanner;
    public class Demo0505 {
        public static void main(String[] args) {
            System.out.println("请您输入一个你想删除的文件或目录:");
            String s1=new Scanner(System.in).nextLine();
            File file=new File(s1);
            boolean flag=file.delete();
            if (flag){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }
    }

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------

    感谢到访!期待您的下次光临!

     


     
     
  • 相关阅读:
    I.MX6ULL LED C程序(转自左忠凯)
    I.MX6ULL的LED汇编程序
    Linux中的信号
    springboot的模板引擎之简介区分(一)
    springboot常用Starter介绍
    springboot拦截器之Filter和拦截器Interceptor之间的区别(六)
    springboot拦截器之多个自定义拦截器Interceptor的执行顺序(五)
    springboot拦截器之自定义拦截器Interceptor以及新旧配置的对比(四)
    springboot拦截器之自定义监听器listener(三)
    springboot拦截器之自定义原生servlet(二)
  • 原文地址:https://www.cnblogs.com/varchar-pig/p/13934430.html
Copyright © 2011-2022 走看看