Simple example of using Java to read/write text files

Source: Internet
Author: User

/*
* Simple example of reading/writing text files
* There are three examples:
* 1. Example of reading a file into memory (stringbuffer here)
* 2. Write the text in the content to a file
* 3. Read the content of one file and write it to another file.
* It also shows that if the content read from the input stream is written to the output stream (Text Stream only)
* The three examples can exist independently, so you can only view one of them as needed.
*/

Import java. Io. bufferedreader;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. Io. outputstreamwriter;
Import java. Io. printstream;
Import java. Io. printwriter;

Public final class accesstextfile {

/**
* 1. Read the text in the stream into a stringbuffer.
* @ Throws ioexception
*/
Public void readtobuffer (stringbuffer buffer, inputstream is)
Throws ioexception {
String line; // used to save the content read by each row
Bufferedreader reader = new bufferedreader (New inputstreamreader (is ));
Line = reader. Readline (); // read the first row
While (line! = NULL) {// If line is null, the read is complete.
Buffer. append (line); // Add the read content to the buffer.
Buffer. append ("\ n"); // Add a line break
Line = reader. Readline (); // read the next row
}
}

/**
* 2. Demonstrate reading the content in stringbuffer into the stream
*/
Public void writefrombuffer (stringbuffer buffer, outputstream OS ){
// Use printstream to conveniently output content to the output stream
// Its object usage is the same as that of system. Out.
// (System. Out itself is a printstream object)
Printstream PS = new printstream (OS );
PS. Print (buffer. tostring ());
}

/**
* 3 *. copy content from the input stream to the input stream
* @ throws ioexception
*/
Public void copystream (inputstream is, outputstream OS) throws ioexception {
// For the read process, see the comments in readtobuffer
string line;
bufferedreader reader = new bufferedreader (New inputstreamreader (is ));
printwriter writer = new printwriter (New outputstreamwriter (OS);
line = reader. readline ();
while (line! = NULL) {
writer. println (line);
line = reader. readline ();
}< br> writer. flush (); // finally, make sure to write everything in the output stream
// The writer is not closed here because the OS is passed in from outside
// since it is not enabled from here, in this case, the writer is disabled.
// If the writer is closed, the OS encapsulated in the writer is also disabled.
}

/**
* 3. Call the copystream (inputstream, outputstream) method to copy text files.
*/
Public void copytextfile (string infilename, string outfilename)
Throws ioexception {
// Generate the corresponding input/output stream based on the input/output file
Inputstream is = new fileinputstream (infilename );
Outputstream OS = new fileoutputstream (outfilename );
Copystream (is, OS); // use copystream to copy content
Is. Close (); // is enabled here, so you need to close
OS. Close (); // OS is enabled here, so you need to disable
}

Public static void main (string [] ARGs) throws ioexception {
Int Sw = 1; // three test options
Accesstextfile test = new accesstextfile ();

Switch (SW ){
Case 1: // test read
{
Inputstream is = new fileinputstream ("E: \ test.txt ");
Stringbuffer buffer = new stringbuffer ();
Test. readtobuffer (buffer, is );
System. Out. println (buffer); // write the content in the buffer.
Is. Close ();
Break;
}
Case 2: // test write
{
Stringbuffer buffer = new stringbuffer ("only a test \ n ");
Test. writefrombuffer (buffer, system. Out );
Break;
}
Case 3: // test copy
{
Test. copytextfile ("E: \ test.txt", "E: \ r.txt ");
}
Break;
}
}

}

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.