/* * 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 the 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 (); } Writer. flush (); // finally, make sure to write everything in the output stream. // Writer is not disabled because the OS is passed in from outside. // Since it is not opened from here, it will not be closed from here // If the writer is closed, the OS encapsulated in it will be shut down. } /** * 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 testn "); Test. writeFromBuffer (buffer, System. out ); Break; } Case 3: // test copy { Test. copyTextFile ("E: \ test.txt", "E: \ r.txt "); } Break; } } } |