File类:
定义:文件和目录径的抽象表示形式, Java中将路径或者文件封装成File对象
1.File类的静态成员变量
package com.zs.Demo2; import java.io.File; public class FileDemo { public static void main(String[] args) { fun(); } private static void fun() { String s = File.pathSeparator;//目录分隔符 ; 在linux中是: System.out.println(s); //结果 ; String s1 = File.separator;// 目录的名称分隔符 在linux中是:/ System.out.println(s1); //结果 } }
2.File类的构造方法:
import com.sun.org.apache.bcel.internal.generic.NEW; import java.io.File; public class FileDemo1 { public static void main(String[] args) { fun1(); fun2(); fun3(); } private static void fun3() { File parent = new File("d:"); File f=new File(parent,"IntelliJ IDEA 2018.3.4"); System.out.println(f);//d:IntelliJ IDEA 2018.3.4 } private static void fun2() { //传递父路径,子路径 File f=new File("d:","IntelliJ IDEA 2018.3.4"); System.out.println(f);//d:IntelliJ IDEA 2018.3.4 } private static void fun1() { //传递路径名 File f=new File("D:\IntelliJ IDEA 2018.3.4"); System.out.println(f);//D:IntelliJ IDEA 2018.3.4 } }
3.File类的功能
创建和删除:
package com.zs.Demo2; import com.sun.org.apache.bcel.internal.generic.NEW; import java.io.File; import java.io.IOException; /*创建和删除文件夹功能*/ public class FileDemo2 { public static void main(String[] args) { try { fun(); fun1(); fun2(); } catch (IOException e) { e.printStackTrace(); } } private static void fun2() { //删除,不经过回收站,直接从硬盘中删除 File f= new File("d:\Filelx.txt"); boolean b = f.delete(); System.out.println(b);//true } private static void fun1() { //创建单级文件夹 File f=new File("d:\lx"); boolean b = f.mkdir(); System.out.println(b); //创建多级文件夹 File f1=new File("d:\Filelx\lx\javalx.txt"); boolean b1 = f1.mkdirs(); System.out.println(b1); } private static void fun() throws IOException { File f=new File("d:\FileLx.txt"); //createNewFile()创建文件,返回true,如果已经存在了,就不再创建,返回false; boolean b = f.createNewFile(); System.out.println(b);//true } }
获取功能:
package com.zs.Demo2; import java.io.File; public class FileDemo3 { public static void main(String[] args) { fun(); fun1(); fun2(); fun3(); } private static void fun3() { File f=new File("D:\IntelliJ IDEA 2018.3.4\build.txt"); String parent = f.getParent();//获取父路径 System.out.println(parent);//D:IntelliJ IDEA 2018.3.4 } private static void fun2() { File f=new File("D:\IntelliJ IDEA 2018.3.4\build.txt"); String path = f.getPath();//获取文件的绝对路径 System.out.println(path);//D:IntelliJ IDEA 2018.3.4uild.txt } private static void fun1() { File f=new File("D:\IntelliJ IDEA 2018.3.4\build.txt"); long length = f.length();//获取文件的字节数 System.out.println(length);//14 } private static void fun() { File f=new File("D:\IntelliJ IDEA 2018.3.4\build.txt"); String name = f.getName();//获取路径最后部分的名字 System.out.println(name);//build.txt } }
判断功能:
package com.zs.Demo2; import java.io.File; public class FileDemo4 { public static void main(String[] args) { fun(); fun1(); } private static void fun1() { File f=new File("D:\IntelliJ IDEA 2018.3.4"); boolean b = f.isDirectory();//判断是否为文件夹(路径) System.out.println(b);//true } private static void fun() { File f=new File("D:\IntelliJ IDEA 2018.3.4\build.txt"); boolean b = f.exists();//判断文件(夹)是否存在 System.out.println(b);//true } }
遍历目录获取(list获取):
package com.zs.Demo2; import java.io.File; public class FileDemo5 { public static void main(String[] args) { // fun(); fun2(); } private static void fun() { File f=new File("D:\IntelliJ IDEA 2018.3.4"); String[] list = f.list();//获取 目录下的文件以及文件夹 for (String s : list) { System.out.println(s); } /* bin build.txt help Install-Windows-zip.txt ipr.reg jre64 lib license plugins product-info.json redist*/ } private static void fun2() { //同样的功能,推荐使用这种,因为这种功能更强大,可以实现更多后续操作 File f=new File("D:\IntelliJ IDEA 2018.3.4"); File[] files = f.listFiles(); for (File file : files) { System.out.println(file); } /* D:IntelliJ IDEA 2018.3.4in D:IntelliJ IDEA 2018.3.4uild.txt D:IntelliJ IDEA 2018.3.4help D:IntelliJ IDEA 2018.3.4Install-Windows-zip.txt D:IntelliJ IDEA 2018.3.4ipr.reg D:IntelliJ IDEA 2018.3.4jre64 D:IntelliJ IDEA 2018.3.4lib D:IntelliJ IDEA 2018.3.4license D:IntelliJ IDEA 2018.3.4plugins D:IntelliJ IDEA 2018.3.4product-info.json D:IntelliJ IDEA 2018.3.4 edist*/ } }
文件过滤器:
package com.zs.Demo2; import com.sun.org.apache.bcel.internal.generic.NEW; import java.io.File; import java.io.FileFilter; public class FileDemo6 { static class MyFile implements FileFilter { @Override public boolean accept(File pathname) { return pathname.getName().endsWith(".exe"); } } public static void main(String[] args) { fun(); } private static void fun() { File f= new File("D:\IntelliJ IDEA 2018.3.4\bin"); File[] files = f.listFiles(new MyFile()); for (File file : files) { System.out.println(file); } } }
递归遍历一个目录下的所有文件:
package com.zs.ZaoYe; import java.io.File; public class DiGui { DiGui(){ File f=new File("D:\IntelliJ IDEA 2018.3.4"); if (f.exists()){ binaryFile(f); }else{ System.out.println("要遍历的目录不存在!!!"); } } private void binaryFile(File f){ File[] files = f.listFiles(); for (File file : files) { if (file.isDirectory()) { binaryFile(file); } else { System.out.println(file); } } } public static void main(String[] args) { new DiGui(); } }