Filter, read, and write Java I/O files.

Source: Internet
Author: User

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}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.