Java learning "File IO"

Source: Internet
Author: User

Blog: Http://blog.csdn.net/muyang_ren

1. Quickly read a text file and output the contents of the file to a new file;

 PackageLhy.java_day3.oop;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException; Public  class test_bufferead {    Private StaticBufferedReader Bfreader;Private StaticBufferedWriter Bfwriter;Private StaticString str;/** * @param args * *     Public Static void Main(string[] args) {//TODO auto-generated method stub        Try{Bfreader =NewBufferedReader (NewFileReader ("F:/java_day3.txt")); Bfwriter =NewBufferedWriter (NewFileWriter ("F:/write.txt")); while((Str=bfreader.readline ())! =NULL) {System.out.println (str); Bfwriter.write (str+"\ r \ n");            Bfwriter.flush (); }        }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}

2. List of files in a directory with a suffix of ". Java"

 PackageLhy.java_day3.oop;ImportJava.io.File; Public  class test_file {    Private StaticFile FS;/** * @param args * *     Public Static void Main(string[] args) {String pathname ="E:\\test";Try{fs =NewFile (pathname); String[] Str=fs.list (); for(String string:str) {if(String.endswith (". Java")) System.out.println (string); }           }Catch(NullPointerException e) {//Todo:handle exceptionSystem.out.println ("failed to open Directory!"); }       }}

3. Copying picture files

 PackageLhy.java_day3.oop;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.IOException; Public  class test_picture {    Private StaticFileInputStream fis;Private StaticFileOutputStream Fos;Private Static intRetPrivate Static byte[] B =New byte[2048];/** * @param args * *     Public Static void Main(string[] args) {//TODO auto-generated method stub        Try{FIS =NewFileInputStream ("F:\\tu1.jpg"); FOS =NewFileOutputStream ("F:\\tu2.jpg"); while(true) {ret = Fis.read (b,0, b.length);if(ret!=-1) {Fos.write (b,0, ret); }Else{ Break; }                       }        }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}

Java learning "File IO"

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.