Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7.  
  8.  
  9. public class CatFiles
  10. {
  11.  
  12.     public static void main(String[] args)
  13.     {
  14.        
  15.             BufferedReader r = null;
  16.             BufferedWriter w = null;
  17.             int n = args.length;
  18.             int m = n - 1;
  19.             try
  20.             {
  21.                 w = new BufferedWriter(new FileWriter(args[m]));
  22.             }
  23.             catch (IOException e)
  24.             {
  25.                 System.out.println("cannot write file");
  26.             }
  27.            
  28.             for (int i = 0; i < n; i++)
  29.             {
  30.                 try
  31.                 {
  32.                     r = new BufferedReader(new FileReader(args[i]));
  33.                 }
  34.                 catch (FileNotFoundException e)
  35.                 {
  36.                     System.out.println("file not found");
  37.                 }
  38.            
  39.                 try
  40.                 {
  41.                     String line = r.readLine();
  42.                     while(line != null)
  43.                     {
  44.                         w.write(line);
  45.                         r.readLine();
  46.                     }
  47.                 }
  48.                 catch (IOException e)
  49.                 {
  50.                     System.out.println("cannot write this file");
  51.                 }
  52.                 try
  53.                 {
  54.                     r.close();
  55.                 }
  56.                 catch (IOException e)
  57.                 {
  58.                     System.out.println("cannot close reader");
  59.                 }
  60.                
  61.             }
  62.            
  63.             try
  64.             {
  65.                 w.close();
  66.             }
  67.             catch (IOException e)
  68.             {
  69.                 System.out.println("cannot close writer");
  70.             }
  71.    
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement