/*
* 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;
}
}
}