Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 17th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //PE 10 - Character File I/O - Bertram Byam Jr - Due: 4/12/10
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. class Copy{
  6.         public static void main(String []args){
  7.                
  8.                 //Create a file instance
  9.                 File txtFile = new File(args[1]);
  10.                 Scanner scan = new Scanner(System.in);
  11.                
  12.                 if(args.length == 2){
  13.                
  14.                         System.out.println("Does it exist " + txtFile.exists());
  15.                        
  16.                         if(txtFile.exists() == true){  
  17.                                 System.out.println("Enter (r) to write,(a) to append, or (x) to exit ");
  18.                                 String choice = scan.nextLine();
  19.                                
  20.                                 if(choice.equals("r"));{
  21.                                         try{
  22.                                                 FileReader in = new FileReader(args[0]);
  23.                                                 FileWriter out = new FileWriter(args[1]);
  24.                                                
  25.                                                 int chr = in.read();// read the first character
  26.                                                 while(chr != -1) {
  27.                                                
  28.                                                 out.write(chr);
  29.                                                 chr = in.read();
  30.                                                 }
  31.                                                 out.flush();
  32.                                                 }
  33.                                 catch(IOException e) {
  34.                                                 System.out.println("Exception: " + e.getMessage());
  35.                                                 }
  36.                                         }
  37.                                        
  38.                                 if(choice.equals("a")){
  39.                                         try{
  40.  
  41.                                                 FileReader in = new FileReader(new File(args[0]));
  42.                                                 BufferedReader br = new BufferedReader(in);
  43.                                                 FileWriter out = new FileWriter(new File(args[1]));
  44.                                                 //BufferedWriter bw = new BufferedWriter(out);
  45.                                                 String temp = "";                              
  46.                                                                                                 temp = br.readLine();
  47. System.out.println(temp);
  48. out.append(temp);
  49.  
  50.                                         /*      while(temp != null)
  51.                                                 {
  52.                                                         out.append(temp);
  53.                                                         temp = br.readLine();
  54.                                                 }
  55.                                                 */
  56. /**                                            
  57.                                                 int chr = in.read();// read the first character
  58.                                                 while(chr != -1) {
  59.                                                
  60.                                                
  61.                                                 out.write(chr);
  62.                                                 chr = in.read();
  63.                                                 }
  64.                                                 out.flush();
  65. **/
  66.                                                 }
  67.  
  68.                                 catch(IOException e) {
  69.                                                 System.out.println("Exception: " + e.getMessage());
  70.                                                 }
  71.                                         }
  72.                                 }                              
  73.                 else {
  74.                                 System.out.println("Please enter input & output file names");
  75.                                 }
  76.                        
  77.         }      
  78. }}