This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes fileREader and FileWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "fileREader and FileWriter" Watch "fileREader and FileWriter" New topic
Author

fileREader and FileWriter

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
Hello
I have to modify this program in two ways, and I am a REAL newbie.......PLEASE HELP.
1) Rewrite the program using fileReader and FileWriter streams
2) Write another program with buffered streams to boost performance.

import java.io.*;
public class CopyFileUsingByteStream
{
// Main method: args[0] for sourcefile and args[1] for target file
public static void main(String[] args)
{
// Declare input and output file streams
FileInputStream fis = null;
FileOutputStream fos = null;
// Check usage
if (args.length !=2)
{
System.out.println(
"Usage: java CopyFileUsingByteStream fromfile tofile");
System.exit(0);
}
try
{
// Create file input stream
fis = new FileInputStream(new File(args[0]));
// Create file output stream if the file does not exist
File outFile = new File(args[1]);
if (outFile.exists())
{
System.out.println("file " + args[1] + " already exists");
return;
}
else
fos = new FileOutputStream(args[1]);
// Display the file size
System.out.println("The file " + args[0] + " has "+
fis.available() + " bytes");
// Continuously read a byte from fis and write it to fos
int r;
while ((r = fis.read()) != -1)
fos.write((byte)r);
}
catch (FileNotFoundException ex)
{
System.out.println("File not found: " + args[0]);
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
finally
{
try
{
// Close files
if (fis != null) fis.close();
if (fos != null) fos.close();
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}
}
THANKS !!! Mary Ellen
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
Hello,
1)Using FileReader and FileWriter:
import java.io.*;
public class CopyFileUsingReader
{
public static void main(String[] args)
{
FileReader fr=null;
FileWriter fw=null;
if(args.length!=2)
{
System.out.println("Usage: java CopyFileUsingByteStream fromfile tofile");
System.exit(0);
}
try
{
//Create FileReader
fr=new FileReader(new File(args[0]);
//Create FileWriter
fw=new FileWriter(new File(args[1]));
//Continuously read an int from fr and write to fw
int i=0;
while((i=fr.read())!=-1)
fw.write(i);
fr.close();
fw.close();
}//end of try block
catch(FileNotFoundException e)
{
System.out.println("File Not Found:"+args[0]);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}//end of main
}//end of class
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
2.Using BufferedReader and BufferedWriter
import java.io.*;
public class CopyFileUsingBufferedReader
{
public static void main(String args[])
{
BufferedReader br=null;
BufferedWriter bw=null;
if(args.length!=2)
{
System.out.println(
"Usage: java CopyFileUsingByteStream fromfile tofile");
System.exit(0);
}
try {
//Create BufferedReader
br=new BufferedReader(new FileReader(new File(args[0])));
//Create bufferedwriter
bw=new BufferedWriter(new FileWriter(new File(args[1])));
//read a line from br and write a line to bw
String line=new String();
while((line=br.readLine())!=null)
{
bw.write(line);
bw.newLine();//creates a new line..
}
br.close();
bw.close();
}//end of try block
catch(FileNotFoundException e)
{
System.out.println("File Not Found:"+args[0]);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}//end of main
}//end of class
[This message has been edited by sai challa (edited April 27, 2001).]
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
3.BufferedInputStream and BufferedOutputStream
import java.io.*;
public class CopyFileUsingByteStream
{
// Main method: args[0] for sourcefile and args[1] for target // file
public static void main(String[] args)
{
// Declare input and output file streams
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
// Check usage
if (args.length !=2)
{
System.out.println("Usage: java CopyFileUsingByteStream"+ "fromfile tofile");
System.exit(0);
}
try
{
// Create file input stream
bis =new BufferedInputStream(new FileInputStream(new File(args[0])));
// Create file output stream if the file does not exist
File outFile = new File(args[1]);
if (outFile.exists())
{
System.out.println("file " + args[1] + " already"+ "exists");
return;
}
else
bos = new BufferedOutputStream(new FileOutputStream(new File(args[1])));
// Display the file size
System.out.println("The file " + args[0] + " has "+
bis.available() + " bytes");
// Continuously read a byte from fis and write it to fos
int r;
while ((r = bis.read()) != -1)
bos.write(r);
}//end of try
catch (FileNotFoundException ex)
{
System.out.println("File not found: " + args[0]);
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
finally
{
try
{
// Close files
if (bis != null) bis.close();
if (bos != null) bos.close();
}
catch (IOException ex)
{
System.out.println(ex);
}
}//end of finally
}//end of main
}//end of class

[This message has been edited by sai challa (edited April 27, 2001).]
 
GeeCON Prague 2014
 
subject: fileREader and FileWriter