Writing to a text file
1. Associating read-in files, using reader and FileReader
2. Associating written files, using writer and FileWriter
3. Create a buffered char array for receiving read-through text information
4. Read the text into the buffer array (buff)
5. Output the text information read
6. Write down the Read file
7. Turn off write file stream
8. Turn off Read file stream
PackageCom.machuang.io.charIO;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.Reader;ImportJava.io.Writer; Public classTextwrite { Public Static voidMain (string[] args) {//Connect with DocumentsReader reader =NULL; Writer writer=NULL; Try{Reader=NewFileReader ("F:/win10/test/a.txt"); Writer=NewFileWriter ("F:/win10/test/acopy.txt",true); //creating a char string buffer array Char[] Cbuf =New Char[1024]; //Read and write intLen = 0; while( -1! = (len =Reader.read (CBUF))) {Writer.write (CBUF); Writer.flush (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { Try { if(NULL!=writer) {Writer.close (); } if(NULL!=reader) {Reader.close (); } } Catch(IOException e) {e.printstacktrace (); } } } //Match main function}
java--text File Write