Java training, Android training, iOS training,. NET training </a>, look forward to communicating with you!
File class
1, used to encapsulate files or folders into objects, to facilitate the operation of files and folders.
2. The file object can be passed as a parameter to the constructor of the stream.
File object Features:
Create and delete:
1. Boolean createnewfile (): When and only if there is no file with the name specified by this abstract pathname, a new empty file is not created.
2. Boolean mkdir (): Creates the directory specified by this abstract path name.
3. Boolean mkdirs (): Creates the directory specified by this abstract pathname, including all required but nonexistent parent directories.
4. Boolean delete (): Deletes the file or directory represented by this abstract path name.
5. Boolean deleteonexit (): When the virtual machine terminates, request to delete the file or directory represented by this abstract path name.
Example code:
ImportJava.io.File;Importjava.io.IOException; Public classtest{ Public Static voidMain (String...args)throwsException {File f=NewFile ("C:\\ folder \\1.Java"); File F1=NewFile ("C:\\ folder");
File F2 = new file ("C:" +file.separator+ "1.java"); //separator A system-related default name delimiter, which is represented as a string for convenience. //Create a directory. F1.mkdir (); //Create a folder that automatically creates the root folder and subfolders if there are folders in the incoming file name. F.mkdirs (); /*Call the file creation method, if the directory of the file does not exist, then the exception will be reported, if the file exists, do not create the file, if it does not exist, create the file. */F.createnewfile (); //Rest 4 secondsThread.Sleep (4000); //created a file, and if the virtual machine exits, the file is automatically deleted. F.deleteonexit (); } Public Static voidsop (Object obj) {System.out.println (obj); } }
Judge:
1. Boolean exists (): Tests whether the file or directory represented by this abstract pathname exists.
2. Boolean Isabsolute (): Tests whether this abstract path name is an absolute pathname.
3. Boolean isdirectory (): Tests whether the file represented by this abstract pathname is a directory.
4. Boolean isfile (): Tests whether the file represented by this abstract path name is a standard file.
5. Boolean Ishidden (): Tests whether the file specified by this abstract path name is a hidden file.
Example code:
ImportJava.io.File;Importjava.io.IOException; Public classtest{ Public Static voidMain (String...args)throwsioexception{File F=NewFile ("C:\\1.java"); //whether it is an executable fileSOP (F.canexecute ()); //whether the file existsSOP (F.exists ()); //whether it is a fileSOP (F.isfile ()); //whether it is a directorySOP (F.isdirectory ());
//is a hidden fileSOP (F.ishidden ()); } Public Static void sop (Object obj) {System.out.println (obj); } }
Get:
1. File getabsolutefile (): Returns the absolute pathname of this abstract path name form.
2. String GetAbsolutePath (): Returns the absolute path name string for this abstract pathname.
3. File getcanonicalfile (): Returns the canonical form of this abstract path name.
4. String Getcanonicalpath (): Returns the canonical path name string for this abstract pathname.
5, Long Getfreespace (): Returns the number of unallocated bytes in the partition specified by this abstract path name
6. String getName (): Returns the name of the file or directory represented by this abstract path name.
7. String GetParent (): Returns the pathname string of the parent directory for this abstract pathname, or returns if the pathname does not specify a parent directory null
.
8. File getparentfile (): Returns the abstract path name of the parent directory for this abstract pathname, or returns if the pathname does not specify a parent directory.null
9. String GetPath (): Converts this abstract pathname to a path name string.
10. Boolean Renameto (File dest): Renames the file represented by this abstract path name.
11, Static file[] Listroots (): Lists the available file system roots.
12, string[] list (): Returns an array of strings that specify the files and directories in the directory represented by this abstract pathname.
code example:
Java.io.File; public classtest{ public static void Main (String...args) {file[] File = File.listroots (); for (File f:file) {System.out.println (f );
File F = new File ("g:\\" = F.list (); for (String filename:name) {System.out.println (fileName); }}}
application Example 1:
Requirements: List the code for all content in a directory:
/*lists the files or folders in the specified directory, including the contents of subdirectories. That is, listing everything in the specified directory because there are directories in the directory, just use the same function that lists the directory functions to complete. In the listing process appears or the directory, you can also call this function is the function itself call itself, this form of expression, or programming method, to become recursive recursion to note: 1, limit 2, to pay attention to the number of recursion, to avoid memory overflow*/ ImportJava.io.File; Public classtest{ Public Static voidMain (String...args) {File dir=NewFile ("D:\\demo"); Dir (dir,0); }//functions for file-level evaluation Public StaticString Getlevel (intLevel ) {StringBuilder SB=NewStringBuilder (); for(intx=0;x<level;x++) {sb.append (" "); }returnsb.tostring (); }//functions that list all content in a directory Public Static voidDir (File dir,intLevel ) {SOP (Getlevel (level)+dir); level++; file[] Files=Dir.listfiles (); for(filefile:files) {if(!File.ishidden ()) {if(File.isdirectory ()) dir (file,level); ElseSOP (Getlevel ( level+1) +file); }}} Public Static voidsop (Object obj) {System.out.println (obj); } }
application Example 2:
Requirement: Stores the absolute path of a Java file in a specified directory into a text file.
/*practice storing the absolute path of a Java file in a specified directory into a text file. Create a Java file list file. Idea: 1, recursive to the specified directory. 2, gets the recursive process so the path of the Java file. 3, store these paths in the collection. 4, writes the data in the collection to a file. */ImportJava.io.*;ImportJava.util.*;classjavafilelist{ Public Static voidMain (string[] args)throwsIOException {File dir=NewFile ("d:\\java1223"); List<File> list =NewArraylist<file>(); Filetolist (dir,list); //System.out.println (List.size ());File File=NewFile (dir, "Javalist.txt"); WriteToFile (List,file.tostring ()); }
Public Static voidFiletolist (File dir,list<file>list) {file[] files=Dir.listfiles (); for(File file:files) {if(File.isdirectory ()) filetolist (file,list); Else { if(File.getname (). EndsWith (". Java") ) list.add (file); } } } Public Static voidWriteToFile (list<file> list,string javalistfile)throwsIOException {bufferedwriter BUFW=NULL; Try{BUFW=NewBufferedWriter (NewFileWriter (javalistfile)); for(File f:list) {String path=F.getabsolutepath (); Bufw.write (path); Bufw.newline (); Bufw.flush (); } } Catch(IOException e) {Throwe; } finally { Try { if(bufw!=NULL) Bufw.close (); } Catch(IOException e) {Throwe; } } }}
Summary: note in file:
1. The function of File.speparator is to judge the direction of the slash in different systems.
In Windows, the slash direction is the right diagonal \ \
In Linux, the direction of the slash is left oblique//
2. The delete () method can delete a file or an entire folder (the entire directory) and return the result as a Boolean type.
Whether you create a file, create a directory, or delete a file, it returns true only when the action actually occurs.
Dark Horse Programmer------The summary of file class learning in Java