|
Use BufferedReader and BufferedWriter to copy a file |
|
|
|
Contributed by Howell
|
|
Monday, 12 June 2006 |
|
BufferedWriter: Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. BufferedReader:Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
import java.io.*; public class CopyFile { public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader( new FileReader("line.txt")); BufferedWriter bw = new BufferedWriter( new FileWriter("linenum.txt")); String s, space=" "; int num=0; while (br.ready()) { s=br.readLine(); num++; bw.write(String.valueOf(num)); bw.write(space); bw.write(s); bw.newLine(); } bw.close(); } } |
|
Last Updated ( Monday, 12 June 2006 )
|