Home > Java > Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream – Q35

Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream – Q35

January 18, 2009 Leave a comment Go to comments

Q35: Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream.

import java.io.*;
import javax.swing.*;

class File_RW{

	public static void main(String args[]) throws IOException{
		
		String f_name = JOptionPane.showInputDialog("Enter a file name: ");
		InputStream infile = new FileInputStream(f_name);
		int fsize = infile.available();
		
		f_name = JOptionPane.showInputDialog("Enter a file name to Save As: ");
		OutputStream outfile = new FileOutputStream(f_name, true);
		
		System.out.println("Reading File : " + f_name + "\n");
		char ch;
		for(int i=0; i<fsize; i++){
			ch = (char)infile.read();
			outfile.write(ch);
			System.out.print(ch);
		}
		infile.close();
		outfile.close();
		System.out.println("File Successfully Copied.");
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: