Filter, read, and write Java I/O files.
I. File Filtering
1 public class guolv implements FilenameFilter 2 {3 public static void main (String [] args) 4 {5 File file File = new File ("F: \ java \ workspace \ Fanshe "); // find the file path 6 String [] files = file. list (new guolv (); // enable the recommended array. out. println (files [0] + "= "); 8 9} 10 // If the returned value is true, the File meets the requirements. 11 // If the returned value is false, the File does not meet the requirements. 12 @ Override13 public boolean accept (File dir, String name) 14 {15 if (name. endsWith (". classpath ") 16 {17 return true; 18} else19 {20 return false; 21} 22} 23}
Ii. File Reading
1 public class readers 2 {3 public static void main (String [] args) throws Exception 4 {5 File file = new File ("F: \ java \ workspace \ Fanshe \ src \ com \ cyg \ fanshe. java "); // read file 6 FileInputStream fi = new FileInputStream (file); // create a byte stream to open the file 7 byte [] B = new byte [fi. available ()]; // fi. available can be used to obtain the number of bytes occupied by the file. 8 int a =-1; 9 while (a = fi. read (B ))! =-1) // determine whether the file has reached the end of the file 10 {11 // System. out. println (new String (B); 12} 13 System. out. println (new String (B); 14 // disable stream 15 fi. close (); 16 17} 18}
Iii. File writing
1 public class output 2 {3 public static void main (String [] args) throws Exception 4 {5 File file = new File ("F: \ a.txt "); 6 FileOutputStream out = new FileOutputStream (file); 7 out. write ("abmin ". getBytes (); 8 out. flush (); // clear cache 9 out. close (); // close stream 10} 11}