文件
//第一种 public static void constionmethod1() { //1.File(String pathname)通过给定路径字符串转化为抽象路径名来创建一个File实例 File f = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src\\LearnFile"); //结尾可以以文件结尾也可以以文件夹结尾 //路径可以存在也可以不存在,建立File对象只是把字符串路径封装为File对象,不考虑路径真假 System.out.println(f); } //第二种 public static void constionmethod2() { //1.File(String parent , String child) //根据parent字符串和child字符串创建一个File实例 File f = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src\\LearnFile","Demo01.java"); //好处:把路径分为两部分,父路径和子路径都可以单独书写和变化,使用灵活 System.out.println(f); } //第三种 public static void constionmethod3() { //1.File(File parent , String child) //根据parent抽象路径名和child字符串创建一个File实例 File f = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src"); File f1 = new File(f,"LearnFile\\Demo01.java"); //好处:父路径是File类型,可以使用File的方法对路径进行一些操作,在使用路径创建对象 System.out.println(f1); }
操作文件的基本方法
获取功能
//1.public String getAbsolutePath()返回该File的绝对路径字符串 //1.public String getpath()返回该File的路径字符串(创建时使用相对路径返回相对路径,创建时使用绝对路径返回绝对路径) public static void getmethod1(){ File f1 = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src\\LearnFile\\Demo01.java"); File f2 = new File("testa.txt"); System.out.println(f1.getAbsolutePath()); System.out.println(f2.getAbsolutePath()); System.out.println(f1.getPath()); System.out.println(f2.getPath()); } //3.public String getName()获取构造方法传递路径的结尾部分 public static void getmethod2(){ File f1 = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src\\LearnFile\\Demo01.java"); File f2 = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src\\LearnFile"); System.out.println(f1.getName()); System.out.println(f2.getName()); } //4.public long length()获取文件大小,以字节为单位 public static void getmethod3() { File f1 = new File("C:\\Users\\lenovo\\" + "IdeaProjects\\okok\\Month\\src\\LearnFile\\testc.txt"); long l = f1.length(); System.out.println(l); //注意:1.文件夹没有大小的概念;2.若给出的路径不存在,返回0 }
//判断功能 //1.public boolean exists()判断构造方法中的路径是否存在,存在:true 不存在: false public static void ismethod(){ File f = new File("D:\\作业\\管理统计");//绝对路径 boolean b = f.exists(); System.out.println(b); File f1 = new File("Month"+File.separator+"src\\LearnFile\\testa.txt");//相对路径 b=f1.exists(); System.out.println(b); } //2.public boolean isDirectory 判断给定的路径是否以文件夹结尾 //3.public boolean isFile 判断给定路径是否以文件结尾 public static void ismethod2(String path){ File f = new File(path);//参数传递文件路径 if(f.exists()) {//如果路径不存在,两种方法都会返回false boolean b = f.isDirectory(); boolean b1 = f.isFile(); System.out.println(b); System.out.println(b1); }else{ System.out.println("给出的路径不存在"); } }
//创建与删除功能 //创建功能 //1. public boolean createNewFile()创建一个文件 //2.public boolean mkdir() 创建一个单级文件夹 // public boolean mkdirs() 创建一个多级文件夹(也可以创建一个单级文件夹) /* 注意事项: 1.当且仅当当前文件(夹)不存在时创建一个新的空文件(夹) 2.创建路径在构造方法中给出,路径必须存在,否则会抛出异常 3.返回值 :true 文件不存在,创建文件 false 文件存在,不创建 */ public static void create() throws IOException { System.out.println("请输入创建文件(夹)的路径\n创建文件夹请输入:1,创建文件输入:2;"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); String path = sc.next(); File file = new File(path); if(num==1){ System.out.println("创建多级文件请输入3,单级文件请输入0"); int num2 =sc.nextInt(); if(num2==0){ boolean b=file.mkdir(); if(b){ System.out.println("创建成功!"); }else{ System.out.println("文件已存在,创建失败!!"); } } if(num2==3){ boolean b=file.mkdirs(); if(b){ System.out.println("创建成功!"); }else{ System.out.println("目录已存在,创建失败!!"); } } } if(num==2){ boolean b=file.createNewFile(); if(b){ System.out.println("创建成功!"); }else{ System.out.println("文件已存在,创建失败!!"); } } if(num!=1&&num!=2){ System.out.println("请重新输入"); } } //删除给定路径的文件/文件夹(直接删除硬盘的数据) //public boolean delete() //返回值:true:文件/文件夹删除成功 //false 1.文件夹中有内容;2.路径不存在 public static void deletemethod(String path){ File f =new File(path); boolean b=f.delete(); if(b){System.out.println("删除成功"); }else{System.out.println("删除失败");} }