The file class provides us with a system-related path-cutting character, such as the following for each of the fields
static string PathSeparator: The return value is a string and the result represents a semicolon;
Static char Pathseparatorchar: The return value is of type char and the result is a semicolon
Static char Separatorchar: The return value is a char type, and the result represents \ 's path cut character
static string separator: The return value is of type string, and the result represents \ 's path cut character
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;public class Filedemo {public static void main (string[] args) {String separator=file.separator; SYSTEM.OUT.PRINTLN (separator);//output \ Windows path cut symbol string Pathseparator=file.pathseparator; System.out.println (PathSeparator);//output result is semicolon;}}</span>
with these fields, later on, it is possible to have no cross-system path-cutting characters.
1. Create a file
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;import Java.io.ioexception;public class FileDemo1 {public static void main (string[] args) {File File=new File ("C:" +file.separator+file.separator+ "1.txt"), try {file.createnewfile ();} catch (IOException e) { E.printstacktrace ();}}} </span>
After execution, there will be a 1.txt file in C drive.
2. Delete method
Delete method: Boolean delete () to remove the path encapsulated in the file constructor method. Returns true after the delete succeeds. Note that the deletion is straightforward. Do not take the Recycle Bin
Deferred Delete method void Deleteonexit () removes the path encapsulated in the file construction method, and so on before the JVM finishes removing
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;public class FileDemo1 {public static void main (string[] args) {file File=new file ("C:" + file.separator+ "1.txt"), if (File.exists ()) {File.delete ();} else {System.out.println ("file does not exist");}}} </span>
3. Create a Directory
Using the MkDir method of the File class
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;public class FileDemo1 {public static void main (string[] args) {file File=new file ("C:" + file.separator+ "1"); File.mkdir ();}} </span>
4. List all folders under the file (with hidden files)
String[] List: Gets all directories and files under the path encapsulated in the file construction method
File[] Listfiles: Returns the Folder object under All files. The return value of the two is different
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;public class FileDemo1 {public static void main (string[] args) {file File=new file ("C:" + file.separator+ "Users"); String[] Str=file.list (); for (String s:str) {System.out.println (s);} File[] Files=file.listfiles (); for (file s:files) {System.out.println (s);}}} </span>
5. File filter
The filter effect can be taken when the specified file is obtained under the specified directory. For example, the following methods are used
File[] ListFile (filefilter filter): File filter, can realize want to get the file
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;import Java.io.filefilter;public class Fileflter {public static void main (string[] args) {File fil E = new File ("C:" + File.separator + "1");//Use the form of an anonymous class to encapsulate the FileFilter method file[] files = file.listfiles (new FileFilter () {//own definition Own filtering method public Boolean accept (File pathname) {return pathname.getname (). EndsWith (". txt");}); for (File s:files) {System.out.println (s);}}} </span>
Inference Methods for 6.File classes
~boolean exists (): Infer whether the path encapsulated in the file construction method exists
~boolean isdirectory (): Infer whether a folder is encapsulated in the file construction method
~boolean isfile (): Infer whether files are encapsulated in the file construction method
~boolean Isabslute (): inferred that the file construction method encapsulates an absolute path
~boolean Ishidden (): Infers whether the path encapsulated in the file construction method is a hidden property
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import java.io.file;/* * Test File inferred Method */public class FileExists {public static void main (string[] args) {method ();} Infer that the file construction method encapsulates the absolute path of private static void Method_4 () {File File=new file ("C:\\java"); Boolean b=file.isabsolute (); System.out.println (b);} Infers that the file construction method encapsulates a document that is not a private static void Method_2 () {File File=new file ("C:\\java"), and a Boolean b=file.isfile (); System.out.println (b);} Infers whether the file construction method encapsulates a folder private static void Method_1 () {File File=new file ("C:\\java"); Boolean b=file.isdirectory (); System.out.println (b);} Used to infer whether a path of encapsulation exists in private static void method () {File File=new file ("C:\\Windows"); Boolean b=file.exists (); System.out.println (b);}} </span>
Get method Get start of 7.File class
Static file[] Listroots () returns the current system root
String getName () Gets the File The constructed method encapsulates the file name or directory name, which gets to the end of the encapsulated path, the name of the directory. File
String getParent () Gets the parent path of the path encapsulated by the file construction method. Null is not returned, assuming this method is used. Returns a string object that can use the method of the String class
File getparentfile () Gets the File The parent path of the path that the constructed method encapsulates. Null is not returned , assuming this method is used. Returns a file object that can use The method of the file class
String GetPath () encapsulates the path in the file construction method. becomes a string
String GetAbsolutePath () Gets the absolute path to the path encapsulated in the file construction method. Returns a string
File getabsolutefile () Get File The absolute path of the path encapsulated in the constructor method. Returns the File
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Io;import Java.io.file;public class GetFile {public static void main (string[] args) {method_1 ();} private static void Method_1 () {//Gets the name of the specified file as a filename, file=new file ("C:\\Windows\\System32"); String name=file.getname ();//Gets the parent folder's object path to file Parentfile=file.getparentfile (); System.out.println (Parentfile.tostring ());//The method of obtaining an absolute path string Absolutefile=file.getabsolutepath (); SYSTEM.OUT.PRINTLN (name);} The root folder of the system is generally the drive letter of the operating system C D E, private static void method () {file[] filelist=file.listroots (); for (File o:filelist) { System.out.println (o);}} </span>