Use BufferedReader and BufferedWriter to copy a file

Home arrow Java Tutorials arrow Source code arrow Use BufferedReader and BufferedWriter to copy a file
Use BufferedReader and BufferedWriter to copy a file Print E-mail
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 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground