TheBlackPopeSJ

Untitled

Oct 3rd, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package IT162EM;
  5.  
  6. import java.io.Serializable;
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.ObjectOutputStream;
  14.  
  15. /**
  16.  * @author Ethan Malloy
  17.  * A simple program to write text to a .txt file
  18.  * and print the output to the console
  19.  */
  20. public class VolunteerDataIO implements Serializable {
  21.  
  22.     /**
  23.      * Method to write text to a text file
  24.      * @param txtText the text to be written into the file
  25.      * @throws ClassNotFoundException
  26.      */
  27.     public static void writeTextToFile() throws ClassNotFoundException {
  28.        
  29.         String[] txtRecords = {
  30.                 "First Name     Last Name    City          State    Event \n",
  31.                 "Jack       Black        Cincinnati    OH       Flying Pig \n",
  32.                 "Jason      Gather       Newport       KY       Jazz Festival \n",
  33.                 "Christy        Jackson      Hamilton      OH       5K \n",
  34.                 "Ethan       Malloy       Loveland      OH      10K"
  35.             };
  36.        
  37.         try {
  38.             FileOutputStream fileOutputStream = new FileOutputStream(new File("My Volunteers.txt"));
  39.             ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
  40.            
  41.             //write data to file
  42.             objectOutputStream.writeObject(txtRecords[0]);
  43.             objectOutputStream.writeObject(txtRecords[1]);
  44.             objectOutputStream.writeObject(txtRecords[2]);
  45.             objectOutputStream.writeObject(txtRecords[3]);
  46.             objectOutputStream.writeObject(txtRecords[4]);
  47.            
  48.             objectOutputStream.close();
  49.             fileOutputStream.close();
  50.         }
  51.         catch (FileNotFoundException e) {
  52.             System.out.println("File not found");
  53.         }
  54.         catch (IOException e) {
  55.             System.out.println("Error initializing stream");
  56.         }
  57.     }      
  58.    
  59.    
  60.     /**
  61.      * @param args
  62.      * Main method, takes text and passes it to writeTextToFile().
  63.      */
  64.     public static void main(String[] args) {
  65.    
  66.         // write text into the file
  67.         try {
  68.             writeTextToFile();
  69.         } catch (ClassNotFoundException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment