The Java File class is very powerful, and you can basically perform all operations on files using Java. This article will conduct a detailed analysis of the Java File file operation class and briefly introduce the common methods in the File class. Java developers in need can take a look.
Constructor
The code copy is as follows:
public class FileDemo {
public static void main(String[] args){
//Constructor File(String pathname)
File f1 = new File("c://abc//1.txt");
//File(String parent,String child)
File f2 = new File("c://abc","2.txt");
//File(File parent,String child)
File f3 =new File("c:"+File.separator+"abc");//separator cross-platform separator
File f4 = new File(f3,"3.txt");
System.out.println(f1);//c:/abc/1.txt
}
}
Creation method
1.boolean createNewFile() does not exist and returns true.exist returns false.
2.boolean mkdir() create directory
3.boolean mkdirs() creates multi-level directory
Delete method
1.boolean delete()
2.boolean deleteOnExit() file is deleted after use
The code copy is as follows:
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args){
File f =new File("d://1.txt");
try {
System.out.println(f.createNewFile());//Return false when the file exists
System.out.println(f.delete());//Return false when the file does not exist
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
How to judge
1.boolean canExecute() determines whether the file can be executable
2.boolean canRead() determines whether the file is readable
3.boolean canWrite() determines whether the file is writable
4.boolean exists() determines whether the file exists
5.boolean isDirectory()
6.boolean isFile()
7.boolean isHidden()
8.boolean isAbsolute() can also determine whether the absolute path file does not exist.
How to get it
1.String getName()
2.String getPath()
3.String getAbsolutePath()
4.String getParent()//Return null if there is no parent directory
5.long lastModified()//get the last modified time
6.long length()
7.boolean renameTo(File f)
8.File[] liseRoots()//Get the machine disk letter
9.String[] list()
10.String[] list(FilenameFilter filter)
List files and folders under disk
The code copy is as follows:
public class FileDemo3 {
public static void main(String[] args){
File[] files =File.listRoots();
for(File file:files){
System.out.println(file);
if(file.length()>0){
String[] filenames =file.list();
for(String filename:filenames){
System.out.println(filename);
}
}
}
}
}
File Filtering
The code copy is as follows:
import java.io.File;
import java.io.FilenameFilter;
public class FileDemo4 {
public static void main(String[] args){
File[] files =File.listRoots();
for(File file:files){
System.out.println(file);
if(file.length()>0){
String[] filenames =file.list(new FilenameFilter(){
//file filter directory name file name
public boolean accept(File file,String filename){
return filename.endsWith(".mp3");
}
});
for(String filename:filenames){
System.out.println(filename);
}
}
}
}
}
File[] listFiles()
File[] listFiles(FilenameFilter filter)
List all files with recursion
The code copy is as follows:
public class FileDemo5 {
public static void main(String[] args){
File f =new File("e://sound");
showDir(f);
}
public static void showDir(File dir){
System.out.println(dir);
File[] files =dir.listFiles();
for(File file:files){
if(file.isDirectory())
showDir(file);
else
System.out.println(file);
}
}
}
Move files
Find all the .java files on the d disk, copy them to the c:/jad directory, and modify the types of all files from .java to .jad.
The code copy is as follows:
public class Test5 {
public static void main(String[] args){
File f1 = new File("d://");
moveFile(f1);
}
public static void moveFile(File dir){
File[] files=dir.listFiles();
for(File file:files){
if(file.isDirectory())
moveFile(file);
else{
if(file.getName().endsWith(".java"))
file.renameTo(new File("c://jad//"+
file.getName().substring(0,file.getName().lastIndexOf('.'))+".jad"));
}
}
}
}
The above are all the properties and methods of the Java File class. We just need to simply call the above method to complete the operation of the specified file. I hope this article will be helpful to you.