Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package IT162EM;
- import java.io.Serializable;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- /**
- * @author Ethan Malloy
- * A simple program to write text to a .txt file
- * and print the output to the console
- */
- public class VolunteerDataIO implements Serializable {
- /**
- * Method to write text to a text file
- * @param txtText the text to be written into the file
- * @throws ClassNotFoundException
- */
- public static void writeTextToFile() throws ClassNotFoundException {
- String[] txtRecords = {
- "First Name Last Name City State Event \n",
- "Jack Black Cincinnati OH Flying Pig \n",
- "Jason Gather Newport KY Jazz Festival \n",
- "Christy Jackson Hamilton OH 5K \n",
- "Ethan Malloy Loveland OH 10K"
- };
- try {
- FileOutputStream fileOutputStream = new FileOutputStream(new File("My Volunteers.txt"));
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
- //write data to file
- objectOutputStream.writeObject(txtRecords[0]);
- objectOutputStream.writeObject(txtRecords[1]);
- objectOutputStream.writeObject(txtRecords[2]);
- objectOutputStream.writeObject(txtRecords[3]);
- objectOutputStream.writeObject(txtRecords[4]);
- objectOutputStream.close();
- fileOutputStream.close();
- }
- catch (FileNotFoundException e) {
- System.out.println("File not found");
- }
- catch (IOException e) {
- System.out.println("Error initializing stream");
- }
- }
- /**
- * @param args
- * Main method, takes text and passes it to writeTextToFile().
- */
- public static void main(String[] args) {
- // write text into the file
- try {
- writeTextToFile();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment