Java Learning--file stream FileReader and FileWriter applications

Source: Internet
Author: User

* Copy of text files can be achieved using FileReader and FileWriter.
* For non-text files (video files, audio files, pictures), only the byte stream can be used!

1 @Test2      Public voidTestfilereader () {3FileReader FR =NULL;4         Try {5File File =NewFile ("Dbcp.txt");6FR =Newfilereader (file);7             Char[] C =New Char[24];8             intLen;9              while(len = Fr.read (c))! =-1){TenString str =NewString (c, 0, Len); One System.out.print (str); A             } -}Catch(IOException e) { -             //TODO auto-generated Catch block the e.printstacktrace (); -}finally{ -             if(FR! =NULL){ -                 Try { + fr.close (); -}Catch(IOException e) { +                     //TODO auto-generated Catch block A e.printstacktrace (); at                 } -             } -         } -          -}
1 @Test2      Public voidTestfilereaderwriter () {3         //1. Input stream corresponding file src must exist, otherwise throw an exception. The output stream corresponding to the file dest can not exist, the execution process will be automatically created4FileReader FR =NULL;5FileWriter FW =NULL;6         Try{7             //cannot implement a copy of a non-text file8 //file src = new file ("C:\\users\\shkstart\\desktop\\1.jpg");9 //file Dest = new file ("C:\\users\\shkstart\\desktop\\3.jpg");TenFile src =NewFile ("Dbcp.txt"); OneFile dest =NewFile ("Dbcp1.txt"); A             //2. -FR =NewFileReader (SRC); -FW =NewFileWriter (dest); the             //3. -             Char[] C =New Char[24]; -             intLen; -              while(len = Fr.read (c))! =-1){ +Fw.write (c, 0, Len); -             } +}Catch(Exception e) { A e.printstacktrace (); at}finally{ -             if(FW! =NULL){ -                 Try { - fw.close (); -}Catch(IOException e) { -                     //TODO auto-generated Catch block in e.printstacktrace (); -                 } to             } +             if(FR! =NULL){ -                 Try { the fr.close (); *}Catch(IOException e) { $                     //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); -                 } the             } +         } A}

Java Learning--file stream FileReader and FileWriter applications

Related Article

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.