Summary of java IO file operation methods
- 2020-06-23 00:29:29
- OfStack
Summary of java IO file operation methods
Understanding of input and output:
Input and output, with the program as a reference point, external data into the program, through the input stream to complete. The program passes the data to an external device through an output stream.
File Io operation
// Access to the file
File file=new File("d:/a.txt");
file.getName()--String// The file name
file.getPath()--String// The path
file.getAbsolutePath()--String// The full path
file.getParent()--String// Located folder
file.exists()--Boolean// If there is a is true no false
file.isFile()--Boolean// Is it a file or not
file.isDirectory()--Boolean// Is a directory?
file.isAbsolute()--Boolean Is it a full path
file.canRead()--Boolean// Can be read ?
file.canWrite()--Boolean// Can you write?
file.isHidden()--Boolean// Is it hidden?
file.length()--long// The size of the
System.out.println(new Date(file.lastModified()));// Last revision date
Returns: Tue Feb 07 11:15:09 CST 2017
Create delete file:
file.createNewFile()--Boolean;// Creating it directly does not overwrite the original file
file.delete()--Boolean;// Delete the file
Iterate over all the files under the output file:
public class PrintFiles {
String str="";
public void Get(File dir){
System.out.println(str+dir.getName());
str+=" | ";
print(dir,str);
}
public void print(File dir,String str){
File[] files=dir.listFiles();
for (File file :files) {
if(file.isFile()) {
System.out.println(str + file.getName());
}
if(file.isDirectory()){
Get(file);
}
}
}
public static void main(String[] args) {
PrintFiles p=new PrintFiles();
File file=new File("C:\\Users\\xuezhendong\\Desktop\\ New folder (2)");
p.Get(file);
}
}
Find specific files:
File dir = new File("D:");
File[] files=dir.listFiles(new FileFilter() {// Look for specific files
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith("txt");endwith Said the suffix
}
});
for (File file :
files) {
System.out.println(file);
}
File[] files=dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {//dir The path of the file
//name The file name
return !name.endsWith("a");
}
});
Thank you for reading, I hope to help you, thank you for your support to this site!