Constructor
}
}
1.boolean createNewFile() returns true if it does not exist and false if it exists.
2.boolean mkdir() creates a directory
3.boolean mkdirs() creates multi-level directories
Delete method
1.boolean delete()
2.boolean deleteOnExit() delete the file after completion of use
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();
}
}
}
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() can also determine whether the absolute path file does not exist.
Get method
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 on disk
}
}
File[] listFiles(FilenameFilter filter)
Use recursion to list all 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 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"));
}
}
}
}