Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package iotest;
- /**
- * @author Taimah Williams
- * Date: 2/28/2015
- * Class: CIT 130 15SP Object Oriented Programming - Java BINI/BC01
- * Purpose: Practice I/O
- */
- import java.util.Scanner;
- import java.io.PrintWriter;
- import java.io.FileOutputStream;
- import java.io.FileInputStream;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.EOFException;
- public class IOTest {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- boolean cont = true;
- while(cont == true){
- //Determine what file to test, what type of file it is and what to do with it
- System.out.print("Enter the file name: ");
- String fileName = keyboard.next();
- System.out.println();
- System.out.print("Choose binary or test file (b/t): ");
- String test = keyboard.next();
- System.out.println();
- System.out.print("Choose to read or write (r/w): ");
- String readWrite = keyboard.next();
- System.out.println();
- if(test.equalsIgnoreCase("b")) //test binary file
- {
- if(readWrite.equalsIgnoreCase("r")) //read binary file
- {
- ObjectInputStream inputStream = null;
- System.out.println("File contains: ");
- String line = "";
- try{
- inputStream = new ObjectInputStream(new FileInputStream(fileName));
- while (true){ //Read through file until it throws an exception
- line = inputStream.readUTF();
- System.out.print(line + " ");
- }
- }
- catch (EOFException e){
- System.out.println();
- System.out.println("Reached end of file.");
- }
- catch(IOException e){
- System.out.print("Error could not find/open file.");
- System.exit(0);
- }
- try{
- inputStream.close();
- }
- catch(IOException e){
- System.out.print("Error could not find/open file.");
- System.exit(0);
- }
- }
- else if (readWrite.equalsIgnoreCase("w")) //write binary file
- {
- boolean test2 = true;
- ObjectOutputStream outputStream = null;
- try
- {
- outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
- while(test2 == true)
- {
- keyboard.nextLine(); //For some reason this wouldn't work without this
- System.out.println("Enter a line of information to write to the file: ");
- String line = keyboard.nextLine();
- outputStream.writeUTF(line);
- System.out.print("Enter another line (y/n)? ");
- if(keyboard.next().equalsIgnoreCase("n")){
- test2 = false;
- }
- }
- outputStream.close();
- }
- catch(IOException e){
- System.out.println("Error could not open file.");
- System.exit(0);
- }
- }
- else{ //invalid entry
- System.out.println("Error! Invalid operation");
- continue;
- }
- }
- else if (test.equalsIgnoreCase("t")) //test text file
- {
- if(readWrite.equalsIgnoreCase("r")) //read text file
- {
- Scanner inputStream = null;
- System.out.println("File contains: ");
- try{
- inputStream = new Scanner(new FileInputStream(fileName));
- }
- catch(FileNotFoundException e){
- System.out.print("Error could not find/open file.");
- System.exit(0);
- }
- String line = "";
- while (inputStream.hasNextLine()){
- line += inputStream.nextLine();
- }
- System.out.println(line);
- inputStream.close();
- }
- else if (readWrite.equalsIgnoreCase("w")) // write text file
- {
- boolean test2 = true;
- PrintWriter outputStream = null;
- try{
- outputStream = new PrintWriter(new FileOutputStream(fileName, true));
- }
- catch(FileNotFoundException e){
- System.out.println("Error could not open file.");
- System.exit(0);
- }
- while(test2 == true){
- keyboard.nextLine(); //For some reason this wouldn't work without this
- System.out.println("Enter a line of information to write to the file: ");
- String line = keyboard.nextLine();
- outputStream.print(line);
- System.out.print("Enter another line (y/n)? ");
- if(keyboard.next().equalsIgnoreCase("n")){
- test2 = false;
- }
- }
- outputStream.close();
- }
- else{ //invalid input
- System.out.println("Error! Invalid operation");
- continue;
- }
- }
- else{ //invalid input
- System.out.println("Error! Invalid file type.");
- continue;
- }
- System.out.print("Test another file (y/n)? ");
- if(keyboard.next().equalsIgnoreCase("n"))
- cont = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement