The Java File class is very powerful. 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
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:abc1.txt
}
}
Creation method
1.boolean createNewFile() returns true if it does not exist and false if it exists
2.boolean mkdir() creates directory
3.boolean mkdirs() creates multi-level directories
Deletion method
1.boolean delete()
2.boolean deleteOnExit() Delete the file after completion
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 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();
}
}
}
Judgment method
1.boolean canExecute() determines whether the file is executable
2.boolean canRead() determines whether the file is readable
3.boolean canWrite() determines whether the file can be written
4.boolean exists() determines whether the file exists
5.boolean isDirectory()
6.boolean isFile()
7.boolean isHidden()
8.boolean isAbsolute() determines whether it is an absolute path or the file does not exist
How to get it
1.String getName()
2.String getPath()
3.String getAbsolutePath()
4.String getParent()//If there is no parent directory, return null
5.long lastModified()//Get the time of the last modification
6.long length()
7.boolean renameTo(File f)
8.File[] liseRoots()//Get the machine drive letter
9.String[] list()
10.String[] list(FilenameFilter filter)
List files and folders under the 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);
}
}
}
}
}
文件过滤
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 过滤目录 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)
利用递归列出全部文件
public class FileDemo5 {
public static void main(String[] args){
File f =new File("e:\音樂");
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 in the d drive, copy them to the c:jad directory, and change the types 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"));
}
}
}
}
The above are all the properties and methods of the Java File class. We only need to simply call the above methods to complete the operation of the specified file. I hope this article will be helpful to you.