Summary of common methods for the Java File class
- 2020-05-16 06:23:26
- OfStack
The Java File class is very powerful, with Java you can do almost anything with a file. This article will analyze the Java File file manipulation class in detail, and introduce the common methods in the File class briefly. Developers of Java who need it can have a look at 1.
The constructor
public class FileDemo {
public static void main(String[] args){
// The 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
}
}
Create method
1.boolean createNewFile() does not exist to return true does exist to return false
2.boolean mkdir() create the directory
3.boolean mkdirs() creates multilevel directories
Delete methods
1.boolean delete()
2.boolean deleteOnExit() files are deleted after use
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());// Returns when the file exists false
System.out.println(f.delete());// Returns when the file does not exist false
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Judgment method
1.boolean canExecute() determines whether the file is executable
2.boolean canRead() determines whether the document is readable
3.boolean canWrite() determines whether the document is writable
4.boolean exists() determines whether the file exists
5.boolean isDirectory()
6.boolean isFile()
7.boolean isHidden()
8.boolean isAbsolute() can determine if the absolute path file does not exist
Access method
1.String getName()
2.String getPath()
3.String getAbsolutePath()
4.String getParent()// return null if there is no parent directory
5.long lastModified()// gets the time of the last modification
6.long length()
7.boolean renameTo(File f)
8.File[] liseRoots()//
9.String[] list()
10.String[] list(FilenameFilter filter)
Lists files and folders on disk
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 filter
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 The 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)
Lists all files recursively
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 out all the.java files under d disk, copy to c:\jad, and change the type of all files from.java to.jad.
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"));
}
}
}
}
Those are all the properties and methods of the Java File class. We can simply call the above methods to complete the operation on the specified file.