1.File Profile
/ ** * java.io.File * File is used to indicate the file system of a file or directory * * we can use File: * 1: Access the properties file or directory it represents (name, size, etc.) * 2 : create, delete files or directories * 3: access child a directory * . * but can not access the file data * / public class FileDemo { public static void main (String [] args) { / * * create a file to specify path * path in two ways: absolute and relative paths * absolute path is not generally applicable, although clear, * but can not do cross-platform * * relative path can not directly reflect the actual position, but * flexible and adapt to different operating environment. * execution is in eclipse, "./" in the current directory * refers to the current project directory * / File File= New new File ( "./ demo.txt" ); // get the file name String name = file.getName (); System.out.println (name); // get the length (in bytes) Long len = File .length (); System.out.println (len + "bytes" ); // Get the absolute path String path = file.getAbsolutePath (); System.out.println (path); / * * if readable, write, whether it is a hidden file * / boolean cr = file.canRead (); boolean CW = file.canWrite (); boolean ih =file.isHidden (); System.out.println ( "read:" + Cr); System.out.println ( "write:" + CW); System.out.println ( "hidden files:" + the IH) ; } }
2. Create a file
/ ** * Create a file using the File * / public class CreateNewFileDemo { public static void main (String [] args) throws IOException { / * * Create a new file in the current directory project test.txt * / File File = new new File ( "./test.txt" ); / * * boolean eXISTS () * to determine the current file or directory indicated whether file * already exists, there is a true return * / iF (! File.Exists ()) { // create the file file.createNewFile (); System.out.println ( "file has been created!" ); } The else { System.out.println ( "file already exists!" ); } } }
3. Delete Files
/ ** * Delete a file * / public class DeleteFileDemo { public static void main (String [] args) { / * * the test.txt file in the current directory to delete * / File File = new new File ( "./ the Test. TXT " ); IF (File.Exists ()) { File.delete (); System.out.println ( " file has been deleted! " ); } the else { System.out.println ( " file does not exist "! ); } } }
4. Create a directory
/ ** * Create a directory * / public class MkdirDemo { public static void main (String [] args) { / * * Create a new directory in the current directory named demo * / File dir = new new File ( "./ demo " ); IF (! ) dir.exists () { dir.mkdir (); System.out.println ( " directory has been created! " ); } the else { System.out.println ( " Contents already exist "! ); } } }
5. Create a multi-level directory
/ ** * Create a multi-level directory * / public class MkdirsDemo { public static void main (String [] args) { / * * a new directory in the current directory: A / B / C / D / E / F * / File the dir = new new File ( "./ a / B / C / D / E / F" ); IF (! dir.exists ()) { // the absence of the parent directory is created with out dir.mkdirs (); the System .out.println ( "directory has been created!" ); } the else { System.out.println ( "directory already exists!" ); } } }
6. Delete directory
/ ** * Delete the directory * / public class DeleteDirDemo { public static void main (String [] args) { // remove the current directory in the demo directory File dir = new new File ( "./ demo" ); IF (dir.exists ()) { / * * delete a directory that is a prerequisite. * directory is an empty directory. * / dir.delete (); System.out.println ( "directory has been deleted!" ); } the else { System. Out.println ( "directory does not exist!" ); } } }
7. access to all children of a directory
/ ** * all children access to a directory * / public class ListFilesDemo { public static void main (String [] args) { // get the current directory of all the children File dir = new new File ( "." ); / * * Boolean isFile () * is determined whether the file represented by this file * * Boolean the isDirectory () * is determined whether the current directory represented file * / iF (dir.isDirectory ()) { / * * file [] the listFiles ( ) * get all the children of the current directory, each array * is one element of a child * / File [] SUBS= dir.listFiles(); for(int i=0;i<subs.length;i++) { File sub = subs[i]; System.out.println(sub.getName()); } } } }
8. The child directory satisfy the filter requirements acquires
/ ** * Gets a specified directory subkey * File [] the listFiles (the FileFilter filter) * child directory that meet the filter requirements acquisition * / public class ListFilesDemo2 { public static void main (String [] args) { / * * get the current directory of all the names that begin with the content. "" * / File dir = new new File ( "." ); IF (dir.isDirectory ()) { // the MyFilter the MyFilter new new filter = (); // anonymous inner classes defined filter the FileFilter filter = new new the FileFilter () { public Boolean accept(File file) { return file.getName().startsWith("."); } }; //回调模式 File[] subs = dir.listFiles(filter); System.out.println(subs.length); for(int i=0;i<subs.length;i++) { File sub = subs[i]; System.out.println(sub.getName()); } } } } class MyFilter implements FileFilter{ public boolean accept(File file) { String name = file.getName(); System.out.println("正在过滤:"+name); return name.startsWith("."); } }
9. recursive directory delete
public class the Test { public static void main (String [] args) { File the dir = new new File ( "./ A" ); Delete (the dir); } / * * first determines whether the directory file represented delete * If the file deleted directly, if the current directory should * get the list of all the sub-items and delete one by one and then delete * the directory. * / public static void the delete (File File) { IF (file.isDirectory ()) { // first the directory empty // 1 get all the children of the directory File [] = SUBS File.listFiles (); // 2 deleted one by one child for ( int I = 0; I <subs.length; I ++ ) { File Sub = SUBS [I]; / * * recursive call: a method call inside . * phenomena their methods * / Delete (Sub); } } File .Delete (); } }