Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

fileREader and FileWriter

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
    Bookmark Topic Watch Topic
  • New Topic