Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1.  
  2. package iotest;
  3.  
  4. /**
  5.  * @author Taimah Williams
  6.  * Date: 2/28/2015
  7.  * Class: CIT 130 15SP Object Oriented Programming - Java BINI/BC01
  8.  * Purpose: Practice I/O
  9.  */
  10.  
  11. import java.util.Scanner;
  12. import java.io.PrintWriter;
  13. import java.io.FileOutputStream;
  14. import java.io.FileInputStream;
  15. import java.io.ObjectOutputStream;
  16. import java.io.ObjectInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.io.IOException;
  19. import java.io.EOFException;
  20.  
  21. public class IOTest {
  22.  
  23.     public static void main(String[] args) {
  24.         Scanner keyboard = new Scanner(System.in);
  25.         boolean cont = true;
  26.        
  27.         while(cont == true){
  28.             //Determine what file to test, what type of file it is and what to do with it
  29.             System.out.print("Enter the file name: ");
  30.             String fileName = keyboard.next();
  31.             System.out.println();
  32.            
  33.             System.out.print("Choose binary or test file (b/t): ");
  34.             String test = keyboard.next();
  35.             System.out.println();
  36.            
  37.             System.out.print("Choose to read or write (r/w): ");
  38.             String readWrite = keyboard.next();
  39.             System.out.println();
  40.            
  41.             if(test.equalsIgnoreCase("b")) //test binary file
  42.             {
  43.                 if(readWrite.equalsIgnoreCase("r")) //read binary file
  44.                 {
  45.                     ObjectInputStream inputStream = null;
  46.                     System.out.println("File contains: ");
  47.                     String line = "";
  48.                    
  49.                     try{
  50.                         inputStream = new ObjectInputStream(new FileInputStream(fileName));                
  51.                        
  52.                         while (true){  //Read through file until it throws an exception
  53.                             line = inputStream.readUTF();
  54.                             System.out.print(line + " ");
  55.                         }
  56.                     }
  57.                     catch (EOFException e){
  58.                         System.out.println();
  59.                         System.out.println("Reached end of file.");
  60.                     }
  61.                     catch(IOException e){
  62.                         System.out.print("Error could not find/open file.");
  63.                         System.exit(0);
  64.                     }
  65.                    
  66.                     try{
  67.                         inputStream.close();
  68.                     }
  69.                     catch(IOException e){
  70.                         System.out.print("Error could not find/open file.");
  71.                         System.exit(0);
  72.                     }
  73.                 }
  74.                
  75.                 else if (readWrite.equalsIgnoreCase("w")) //write binary file
  76.                 {
  77.                     boolean test2 = true;
  78.                     ObjectOutputStream outputStream = null;
  79.                    
  80.                     try
  81.                     {
  82.                         outputStream = new ObjectOutputStream(new FileOutputStream(fileName));  
  83.                        
  84.                         while(test2 == true)
  85.                         {
  86.                             keyboard.nextLine(); //For some reason this wouldn't work without this  
  87.                             System.out.println("Enter a line of information to write to the file: ");
  88.                             String line = keyboard.nextLine();
  89.                             outputStream.writeUTF(line);
  90.  
  91.                             System.out.print("Enter another line (y/n)? ");
  92.                             if(keyboard.next().equalsIgnoreCase("n")){
  93.                                 test2 = false;
  94.                             }
  95.                         }
  96.                         outputStream.close();
  97.                     }
  98.                     catch(IOException e){
  99.                         System.out.println("Error could not open file.");
  100.                         System.exit(0);
  101.                     }
  102.                 }
  103.                 else{ //invalid entry
  104.                     System.out.println("Error! Invalid operation");
  105.                     continue;
  106.                 }
  107.             }
  108.            
  109.             else if (test.equalsIgnoreCase("t")) //test text file
  110.             {
  111.                 if(readWrite.equalsIgnoreCase("r")) //read text file
  112.                 {
  113.                     Scanner inputStream = null;
  114.                     System.out.println("File contains: ");
  115.                    
  116.                     try{
  117.                         inputStream = new Scanner(new FileInputStream(fileName));
  118.                     }
  119.                     catch(FileNotFoundException e){
  120.                         System.out.print("Error could not find/open file.");
  121.                         System.exit(0);
  122.                     }
  123.                    
  124.                     String line = "";
  125.                     while (inputStream.hasNextLine()){
  126.                         line += inputStream.nextLine();
  127.                     }
  128.                    
  129.                     System.out.println(line);
  130.                     inputStream.close();
  131.                 }
  132.                
  133.                 else if (readWrite.equalsIgnoreCase("w")) // write text file
  134.                 {
  135.                     boolean test2 = true;
  136.                     PrintWriter outputStream = null;
  137.                    
  138.                     try{
  139.                         outputStream = new PrintWriter(new FileOutputStream(fileName, true));  
  140.                     }
  141.                     catch(FileNotFoundException e){
  142.                         System.out.println("Error could not open file.");
  143.                         System.exit(0);
  144.                     }
  145.                    
  146.                     while(test2 == true){
  147.                         keyboard.nextLine();  //For some reason this wouldn't work without this                      
  148.                         System.out.println("Enter a line of information to write to the file: ");
  149.                        
  150.                         String line = keyboard.nextLine();
  151.                        
  152.                         outputStream.print(line);
  153.  
  154.                         System.out.print("Enter another line (y/n)? ");
  155.                         if(keyboard.next().equalsIgnoreCase("n")){
  156.                             test2 = false;
  157.                         }
  158.                     }
  159.                     outputStream.close();
  160.                 }
  161.                
  162.                 else{ //invalid input
  163.                     System.out.println("Error! Invalid operation");
  164.                     continue;
  165.                 }
  166.             }
  167.            
  168.             else{ //invalid input
  169.                 System.out.println("Error! Invalid file type.");
  170.                 continue;
  171.             }    
  172.            
  173.             System.out.print("Test another file (y/n)? ");
  174.             if(keyboard.next().equalsIgnoreCase("n"))
  175.                 cont = false;
  176.         }
  177.        
  178.     }
  179.    
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement