The Io of input and output operations in Java summarize the of two

  • 2020-04-01 01:21:25
  • OfStack

Operation of file
In this section we discuss the operation of the file itself
Don't waste spittle, speak in code...
Example 1: create a file object
 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
//Create the file path and name to operate on
//Where file.separator represents the system-related separator, Linux: / Windows: \
//Path represents the parent directory in this program and does not contain child files
String path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator; 
//ChildPath represents a subdirectory in this program that contains child files
String childPath = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "demo.txt"; 
//The File object is constructed by separating the parent directory from the child files
//New File("/home/siu/work","test.txt");
File f1 = new File(path,"test.txt"); 
//Use the absolute path to construct the File object
//It can also be written as new File("/home/siu/work/demo.txt");
File f2 = new File(childPath); 
//Creates a file object for the parent directory
File d = new File(path); 
//Build new File objects using existing parent directory objects and child files
File f3 = new File(d,"hello.txt"); 
System.out.println("f1 The path of the " + f1); 
System.out.println("f2 The path of the " + f2); 
System.out.println("f3 The path of the " + f3); 
} 
} 

After compilation, the absolute path to which each File object points is displayed

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173515.png ">  
Instance 2: create and delete files

 
import java.io.File; 
import java.io.IOException; 
public class Demo { 
public static void main(String[] args) { 
String Path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "demo.txt"; 
File f = new File(Path); 
try { 
 
//True is returned if the creation succeeds
//If the file already exists, it is not created successfully, return flase, don't think it will be overwritten
System.out.println(" Create a file :" + f.createNewFile()); 
//Delete the file, return true on success, otherwise return flase
System.out.println(" Delete file: " + f.delete()); 
//This method represents deleting a file when the virtual machine exits
//The reason is that an exception may occur while the program is running and cause a direct exit
//It is necessary to clean up the residue ~!
f.deleteOnExit(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 

You see, it's created, so it returns true, because it's created, so it's deleted

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173516.png ">
Example 3: judgment and testing of files

 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
String Path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "Demo.txt"; 
File f = new File(Path); 
//Determines whether the file is executable
System.out.println("f Is it executable :" + f.canExecute()); 
//Determine if the file exists
System.out.println("f If there is a :" + f.exists()); 
//Determines whether the file is readable
System.out.println("f Readable: " + f.canRead()); 
//Determines whether the file is writable
System.out.println("f Can I write: " + f.canWrite()); 
//Determines if the file is an absolute pathname
System.out.println("f Whether the absolute path: " + f.isAbsolute()); 
//Determines whether the file is a standard file
System.out.println("f Is it a standard document: " + f.isFile()); 
//Determines if the file is a directory
System.out.println("f Is it directory: " + f.isDirectory()); 
//Determines if the file is hidden
System.out.println("f Whether to hide: " + f.isHidden()); 
} 
} 

You can use different files for testing, and it's easy to set file properties, right
It is important to note that if you test with isFlie() and isDirectory(), you first determine whether the file object has been created

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173517.png ">
Example 4: create a directory

 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
String path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator; 
//Path exists here as the parent directory
File f1 = new File(path,"/abc"); 
File f2 = new File(path,"/d/e/f/g"); 
//Create a directory
System.out.println(f1.mkdir()); 
//Create directories recursively
System.out.println(f2.mkdirs()); 
} 
} 

Look at the path

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173518.png ">
Example 5: get file information

 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
String path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "demo.txt"; 
File f = new File(path); 
//Returns the absolute path to the file
//Here the return value is String
System.out.println("f Absolute path name: " + f.getAbsolutePath()); 
//Returns the absolute path to the file
//The return value is File
System.out.println("f Absolute path object: " + f.getAbsoluteFile()); 
//Returns the name of the file or directory
System.out.println("f The name of the: " + f.getName()); 
//Returns the relative path of the file
//What path is encapsulated in the constructor, and what path is returned
System.out.println("f The path: " + f.getPath()); 
//Returns the path to the parent directory
//If the path in the constructor is not an absolute path, then null is returned here
System.out.println("f Parent directory: " + f.getParent()); 
} 
} 

These are commonly used and functionally similar methods, but the less commonly used information can be referred to the API

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173519.png ">
Example 6: lists the root directory of the file system

 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
//ListRoots () is a static method that returns an array of files
File[] files = File.listRoots(); 
//The foreach loop prints the File object
for (File x : files) { 
System.out.println(x); 
} 
} 
} 

Since the local environment is Linux, the root directory has only one /, and if it is Windows, you can list all of your disk characters

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173520.png ">
Example 7: lists all the files in the directory

 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
String path = File.separator + "opt" + File.separator; 
File f = new File(path); 
//Method 1: list()
//Returns an array of strings containing all the file names in the specified directory
//Returns null if it is not a directory
String[] files = f.list(); 
for (String x : files) { 
System.out.println(x); 
} 
//Method 2: listFiles()
//Returns an array of files
/* 
File[] files = f.listFiles(); 
for (File x : files) { 
//If you need to include the path, just print the x
System.out.println(x.getName()); 
} 
*/ 
} 
} 

PIC
Both return all the file names in the directory, but the second is more practical, laying the groundwork for listing files recursively

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173521.png ">
Example 8: recursively lists all files in a directory

 
import java.io.File; 
public class Demo { 
public static void main(String[] args) { 
String path = File.separator + "opt" + File.separator; 
File f = new File(path); 
//Call the following recursive method
print(f); 
} 
//Print a list of directories recursively
public static void print(File f) { 
if(f.isDirectory()){ 
File[] files = f.listFiles(); 
for(File x : files) { 
print(x); 
} 
} else { 
System.out.println(f); 
} 
} 
} 

Well, there's too much to print, just meaning

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010173522.png ">
So much for the basic operation of the file, and I thought of adding more...
The following sections will cover additional operations for Java IO


Related articles: