Java file operations: read and write by row

Source: Internet
Author: User
Tags string to file

Java file operations: read and write by row

 

File operations are the most common part in any language, and Java is no exception. This section mainly introduces the operations on reading files and writing files by row.

 

ImportJava. Io. bufferedreader;

ImportJava. Io. bufferedwriter;

ImportJava. Io. filenotfoundexception;

ImportJava. Io. filereader;

ImportJava. Io. filewriter;

ImportJava. Io. ioexception;

 

 

Public ClassJavafile {

Public Static VoidMain (string [] ARGs ){

Try{

// Read File Content from File

Stringbuffer sb =NewStringbuffer ("");

Filereader reader =NewFilereader ("C: // test.txt ");

Bufferedreader BR =NewBufferedreader (Reader );

String STR =Null;

While(STR = Br. Readline ())! =Null){

SB. append (STR + "/N ");

System.Out. Println (STR );

}

BR. Close ();

Reader. Close ();

// Write string to file

Filewriter writer =NewFilewriter ("C: // test2.txt ");

Bufferedwriter BW =NewBufferedwriter (writer );

Bw. Write (sb. tostring ());

Bw. Close ();

Writer. Close ();

}

Catch(Filenotfoundexception e ){

E. printstacktrace ();

}

Catch(Ioexception e ){

E. printstacktrace ();

}

}

 

}

 

 

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.