Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.BufferedReader;
  7. import java.io.OutputStreamWriter;
  8. import java.util.Scanner;
  9. import java.io.BufferedWriter;
  10.  
  11. public class Main {
  12.    
  13.     static BufferedReader input;
  14.     static BufferedWriter output;
  15.     public static void main(String[] args  ) {
  16.        
  17.        
  18.          
  19.         try {
  20.         File f = new File("student.txt");
  21.         FileInputStream fis = new FileInputStream(f);
  22.         InputStreamReader isr = new InputStreamReader(fis);
  23.         input = new BufferedReader(isr);
  24.          
  25.         char choice = 'R' ;
  26.         boolean access = false;
  27.        
  28.         File file2 = new File("student2.txt");
  29.         if(file2.exists()) {
  30.             System.out.println("The File Already Exists in The Directory , Please Enter A To Append or R to rewrite");
  31.             choice = (char)System.in.read();
  32.            
  33.         }
  34.        
  35.         access = (choice == 'A')?true:false;
  36.         System.out.println(choice);
  37.         System.out.println(access);
  38.         FileOutputStream fos = new FileOutputStream(file2,access);
  39.         OutputStreamWriter osw  = new OutputStreamWriter(fos);
  40.          output = new BufferedWriter(osw);
  41.        
  42.         while(input.ready()) {
  43.             String s = input.readLine();
  44.             String [] lines = s.split(",");
  45.            
  46.             for (int i = 0; i < lines.length; i++) {
  47.                 output.write(lines[i]);
  48.                 output.newLine();
  49.             }
  50.         }
  51.        
  52.         }
  53.        
  54.         catch(IOException e) {
  55.             System.out.println(e);
  56.         }
  57.        
  58.         finally {
  59.             try {
  60.             input.close();
  61.             output.close();
  62.            
  63.             }
  64.             catch(IOException e) {
  65.                 System.out.println(e);
  66.             }
  67.         }
  68.        
  69.        
  70.        
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement