File类
用来给文件或者目录封装成对象
方便对文件与目录的属性信息进行操作
File对象能够作为參数传递给流的构造函数
一、构造函数和分隔符
public static void FileDemo() {//构造函数演示 //能够将一个已存在或不存在的文件或文件夹封装成File对象 File file = new File("d:\a.txt"); File file2 = new File("d:","a.txt"); File file3 = new File("d:\"); File file4 = new File(file3,"a.txt"); File f = new File("d:\sd\a.txt");//不同操作系统上的分隔符不同,Windows下是\ File f2 = new File("d:"+File.separator+"sd"+File.separator+"a.txt");//和上一句等同,在全部的 //File.separator方法实际上就是System.getProperty(....); System.out.println(f2); }
二、类方法
1.获取
获取文件名:File.getName()
获取文件路径:File.getpath()/getAbsolutePath()
获取文件大小:File.length()
获取文件改动时间:File.lastModified()
public static void FileMethodDemo() throws IOException { File file = new File("A.txt"); String filename = file.getName();//获取文件名 String absfilepath = file.getAbsolutePath();//获取绝对路径 String path = file.getPath();//获取相对路径 long len = file.length();//文件大小 long time = file.lastModified();//最后一次改动时间 Date date = new Date(time); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); String time1 = df.format(date); String parent = file.getParent();//得到父文件夹 System.out.println("name : "+filename); System.out.println("abspath : "+absfilepath); System.out.println("path : "+path);//new File("xxx"),xxx是什么就是什么 System.out.println("filelen : "+len); System.out.println("filetime : "+time1); System.out.println("filepatent : "+parent); <span style="white-space:pre"> </span>}
2.创建和删除
public static void FileMethodDemo() throws IOException { //文件的创建和删除 //创建 File file = new File("file.txt"); boolean falg = file.createNewFile(); System.out.println("flag : "+falg);//和输出流不同,假设不存在,就创建,否则。不创建 //删除 boolean flag1 = file.delete(); //boolean falg = file.deleteOnExit();退出时。删除 System.out.println("remove flag: "+flag1); //文件夹的创建和删除 //创建单级文件夹 File dir = new File("ACM"); boolean b = dir.mkdir();//创建单级文件夹 System.out.println("b:"+b); //删除 dir.delete();//Window下要注意,文件夹里有内容删不掉 //多级文件夹 File dir = new File("ACM\asd\as\ad"); dir.mkdirs();//创建多级文件夹 System.out.println(dir.delete());//删除的仅仅是ad。其它是父文件夹 } }
3.推断
public static void FileMethodDemo() throws IOException { //推断 File file = new File("A.txt"); boolean flag = file.exists();//推断A.txt是否存在 boolean flag1 = file.isFile();//推断是否是文件 boolean flag2 = file.isDirectory();//推断是否是文件夹 System.out.println(flag+":"+flag1+":"+flag2); }
4.重命名和剪切
public static void FileMethodDemo() throws IOException { // //重命名 // File file = new File("D:\盛夏光年.mp3"); // File file2 = new File("D:\盛夏光年_1.mp3"); // file.renameTo(file2);//同文件夹下。重命名 //剪切 File file = new File("D:\盛夏光年_1.mp3"); File file2 = new File("G:\盛夏光年.mp3"); file.renameTo(file2);//不同文件夹下,剪切 }
5.获取文件夹内容
public static void FileMethodDemo() throws IOException { File file = new File("c:\"); //File("c:\a.txt");假设不是一个文件夹,而是一个文件,会空指针异常。数组根本不会创建成功 //假设訪问的是系统级文件夹也会发生空指针异常 //File("c:\eqwwrfg")假设文件夹存在,没有内容。会返回一个数组,可是长度为0 //获取当前文件夹下的文件和文件夹的名称和隐藏文件 String[] str = file.list(); for(String p : str){ System.out.println(p); } }
过滤器:
假设仅仅要C盘下的.java文件的话。那就要用到过滤器
list(FilenameFilter filter)
返回一个字符串数组,这些字符串指定此抽象路径名表示的文件夹中满足指定过滤器的文件和文件夹。
FilenameFilter,
实现此接口的类实例可用于过滤器文件名称。
此接口下仅仅有一个方法:
accept(File dir,
String name)
測试指定文件是否应该包括在某一文件列表中。:当且仅当该名称应该包括在文件列表中时返回 true
;否则返回 false
。
import java.io.*; class FilerJava implements FilenameFilter{ public boolean accept(File dir, String name) { // System.out.println(dir+"----"+name); return name.endsWith(".java");//文件后缀 } } public class Main { public static void main(String[] args) throws IOException { FileMethodDemo(); } public static void FileMethodDemo() throws IOException { File file = new File("c:\"); String[] str = file.list(new FilerJava()); for(String p : str){ System.out.println(p); } } }
实际上,过滤器还是先遍历了一边文件夹下全部文件,符合条件的,返回true
listFiles(FileFilter filter)
返回抽象路径名数组。这些路径名表示此抽象路径名表示的文件夹中满足指定过滤器的文件和文件夹。
import java.io.*; class FilerHidden implements FileFilter{ @Override public boolean accept(File pathname) { // TODO Auto-generated method stub return !pathname.isHidden();//显示非隐藏对象 } } public class Main { public static void main(String[] args) throws IOException { FileMethodDemo(); } public static void FileMethodDemo() throws IOException { File dir = new File("C:\"); File[] files = dir.listFiles(new FilerHidden());//得到当前文件夹下全部非隐藏对象 for(File f : files){ System.out.println(f); } } }
过滤器的简单应用
import java.io.*; class FilerHidden implements FilenameFilter{ private String houzhui; public FilerHidden(String houzhui) { super(); this.houzhui = houzhui; } public boolean accept(File dir, String name) { return name.endsWith(houzhui); } } public class Main { public static void main(String[] args) throws IOException { FileMethodDemo(); } public static void FileMethodDemo() throws IOException { File dir = new File("C:\"); File[] files = dir.listFiles(new FilerHidden(".java")); for(File f : files){ System.out.println(f); } } }