File class
Used to encapsulate files or folders into objects
Facilitates the operation of File and folder property information
The file object can be passed as a parameter to the constructor of the stream
First, constructors and separators
public static void Filedemo () {///constructor demo//You can encapsulate a file or directory that already exists or does not exist as a Files object ("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");//different separators on different operating systems, Windows \file F2 = new File ("D:" +file.separator+ "SD" + file.separator+ "A.txt");//And the same as the previous sentence, in all the//file.separator method is actually system.getproperty (...); SYSTEM.OUT.PRINTLN (F2);}
Ii. Types of methods
1. Get
Get file name: File.getname ()
Get file path: Files. GetPath ()/getabsolutepath ()
Get file Size: Files. Length ()
Get file modification time: Files. LastModified ()
public static void Filemethoddemo () throws IOException {File File = new file ("A.txt"); string filename = File.getname ();//Gets the file name string Absfilepath = File.getabsolutepath ();//Gets the absolute path string path = File.getpath ();//Gets the relative path long len = File.length ();//File size long time = file.lastmodified ();//Last modified time Date date = new Date;D Ateformat 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 is what System.out.println ("FileLen:" +len); System.out.println ("FILETIME:" +time1); System.out.println ("filepatent:" +parent); <span style= "White-space:pre" ></SPAN>}
2. Create and delete
public static void Filemethoddemo () throws IOException {//File creation and deletion//creating a File File = new file ("File.txt"); Boolean falg = File . CreateNewFile (); System.out.println ("flag:" +FALG);//and output stream is different, if not present, create, otherwise, do not create//delete Boolean Flag1 = File.delete ();//boolean falg = File.deleteonexit (); When exiting, delete System.out.println ("Remove flag:" +FLAG1);//folder creation and deletion//creation of single-level directory file DIR = new file ("ACM"); Boolean B = Dir.mkdir ();//Create single-level directory System.out.println ("B:" +b);//delete Dir.delete ();//window Note that there is no content in the folder to delete//multilevel directory file Dir = New File ("Acm\\asd\\as\\ad");d ir.mkdirs ();//Create multilevel directory System.out.println (Dir.delete ());//delete only AD, other is parent directory}}
PS: If the file is being streamed, it will not be erased.
3. Judging
public static void Filemethoddemo () throws IOException {//determine file File = new file ("A.txt"); Boolean flag = File.exists ();//Sentenced A.txt If there is a Boolean Flag1 = File.isfile ();//Determine if the file is a Boolean flag2 = File.isdirectory ();//Determine if it is a directory SYSTEM.OUT.PRINTLN (flag + ":" +flag1+ ":" +flag2);}
4. Renaming and clipping
public static void Filemethoddemo () throws IOException {////renamed//file file = new file ("D:\\ Midsummer Light-Years. mp3");//file file2 = new F Ile ("D:\\ Midsummer Light-year _1.mp3");//file.renameto (file2);//directory, rename//cut file File = new file ("D:\\ Summer light Year _1.mp3"); File File2 = new file ("G:\\ Midsummer Lightyear. mp3"); File.renameto (file2);//different directory, cut}
5. Get directory Content
public static void Filemethoddemo () throws IOException {File File = new file ("c:\\");//file ("C:\\a.txt"); If not a directory, but a file , null pointer exception, array will not be created successfully//if access to a system-level directory will also occur null pointer exception//file ("C:\\EQWWRFG") if the directory exists, no content, will return an array, but the length is 0// Gets the name of the file and folder under the current directory and the hidden file string[] str = file.list (); for (String p:str) {System.out.println (P);}}
Filter:
If only the. java file under the C drive is used, then filter
list(FilenameFilter filter)
Returns an array of strings that specify the files and directories in the directory that this abstract path name represents to satisfy the specified filter.
FilenameFilter,
A class instance that implements this interface is available for the filter file name.
There is only one method under this interface:
accept(File dir,String name)
Tests whether the specified file should be included in a list of files. : Returns if and only if the name should be included in the list of files true
false
.
Import Java.io.*;class Filerjava implements Filenamefilter{public Boolean accept (File dir, String name) {//SYSTEM.OUT.PR Intln (dir+ "----" +name); return Name.endswith (". Java");//file suffix}}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);}}}
In fact, the filter goes through all the files in the side directory first, matches the condition, returns True
listFiles(FileFilter filter)
Returns an abstract path to the an array group, which represents the files and directories in the directory that this abstract path name represents for the specified filter.
Import Java.io.*;class Filerhidden implements filefilter{@Overridepublic Boolean accept (File pathname) {//TODO Auto-generated method Stubreturn!pathname.ishidden ()//display non-hidden object}}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 ());//Get all non-hidden objects in the current directory for (File f:files) {System.out.println (f);}}}
Simple Application of filters
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);}}}
Java Learning lesson 52nd-io Stream (vi) file object