Write a program that takes two files names (source and destination) as command line argument .Copy source file’s content to destination file. Use character stream class. Also do same using byte stream and buffer stream.
Using Character Stream:
Code:
Output:
Using Byte Stream:
Code:
Output:
Using Buffer Stream:
Code:
Output:
Using Character Stream:
Code:
import java.io.*;
class S9P3_io_copy_chars
{
public static void main(String args[])
{
String f_name1 = args[0];
String f_name2 = args[1];
FileReader Infile = null;
FileWriter Outfile = null;
int ReadC;
try
{
Infile = new FileReader(f_name1);
Outfile = new FileWriter(f_name2);
while((ReadC = Infile.read()) != -1)
{
System.out.print((char)ReadC);
Outfile.write(ReadC);
}
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found!");
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
Infile.close();
Outfile.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
Output:
Using Byte Stream:
Code:
import java.io.*;
class S9P3_io_copy_bytes
{
public static void main(String args[])
{
String f_name1 = args[0];
String f_name2 = args[1];
FileInputStream Infile = null;
FileOutputStream Outfile = null;
byte ReadB;
try
{
Infile = new FileInputStream(f_name1);
Outfile = new FileOutputStream(f_name2);
do
{
ReadB = (byte)Infile.read();
System.out.print((char)ReadB);
Outfile.write(ReadB);
}
while(ReadB != -1);
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found!");
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
Infile.close();
Outfile.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
Output:
Using Buffer Stream:
Code:
import java.io.*;
class S9P3_io_copy_buffer
{
public static void main(String args[])
{
String f_name1 = args[0];
String f_name2 = args[1];
BufferedReader Infile = null;
BufferedWriter Outfile = null;
int ReadBuff;
try
{
Infile = new BufferedReader(new FileReader(f_name1));
Outfile = new BufferedWriter(new FileWriter(f_name2));
while((ReadBuff = Infile.read()) != -1)
{
System.out.print((char)ReadBuff);
Outfile.write(ReadBuff);
}
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found!");
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
Infile.close();
Outfile.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
Output:
No comments:
Post a Comment