Copy Characters using BufferedStream

26 11 2008

Buffered input streams read data from a memory area known as a buffer;the native input API is called only when the buffer is empty. Similarly,buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
Example

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class CopyFiles {

  public static void main(String[] args){
	  String sourceFileName = "D:\\Test Folder\\Test File.txt";
	  String destinationFileName = "D:\\Test Folder\\Test File 1.txt";
	  copyFile(sourceFileName, destinationFileName);
  }	

 private static void copyFile(String sourceFileName,String destinationFileName) { 

      BufferedReader br = null;
      BufferedWriter bw = null; 

      try {
      	br = new BufferedReader(new FileReader( sourceFileName ));
      	bw = new BufferedWriter(new FileWriter( destinationFileName ));
        int c;
        while ((c = br.read()) != -1)  {
           bw.write(c);
        }
        br.close();
        bw.close();
      }catch (Exception e) {
	  e.printStackTrace();
      }
}
Advertisement

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.