
Untitled
By: a guest on
Apr 17th, 2012 | syntax:
None | size: 1.82 KB | hits: 11 | expires: Never
//PE 10 - Character File I/O - Bertram Byam Jr - Due: 4/12/10
import java.util.*;
import java.io.*;
class Copy{
public static void main(String []args){
//Create a file instance
File txtFile = new File(args[1]);
Scanner scan = new Scanner(System.in);
if(args.length == 2){
System.out.println("Does it exist " + txtFile.exists());
if(txtFile.exists() == true){
System.out.println("Enter (r) to write,(a) to append, or (x) to exit ");
String choice = scan.nextLine();
if(choice.equals("r"));{
try{
FileReader in = new FileReader(args[0]);
FileWriter out = new FileWriter(args[1]);
int chr = in.read();// read the first character
while(chr != -1) {
out.write(chr);
chr = in.read();
}
out.flush();
}
catch(IOException e) {
System.out.println("Exception: " + e.getMessage());
}
}
if(choice.equals("a")){
try{
FileReader in = new FileReader(new File(args[0]));
BufferedReader br = new BufferedReader(in);
FileWriter out = new FileWriter(new File(args[1]));
//BufferedWriter bw = new BufferedWriter(out);
String temp = "";
temp = br.readLine();
System.out.println(temp);
out.append(temp);
/* while(temp != null)
{
out.append(temp);
temp = br.readLine();
}
*/
/**
int chr = in.read();// read the first character
while(chr != -1) {
out.write(chr);
chr = in.read();
}
out.flush();
**/
}
catch(IOException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
else {
System.out.println("Please enter input & output file names");
}
}
}}