Java: Reading/Writing File

Wrote a program that read a file for a company evaluation. I hastily put it together without looking too much at resources so it wasn’t the prettiest of things. Probably should’ve taken my time, so I will now and make it into a post and reference for later.

public class ReadWriteFile {
  public static void main(String[] args) throws IOException {
    BufferedReader in = getReader("hello.txt");
	PrintWriter out = openWriter("hi.txt");
    // file closed automatically when programs exit
	// if the program were to go on need to call, in.close()
	closeFile(in);
	out.close();
  }
 private static BufferedReader getReader(String filename) {

    BufferedReader in = null;
	try {
	  File file = new File(filename);
	  in = new BufferedReader(new FileReader(file));

	} catch (FileNotFoundException e) {
	  System.out.println(filename+" doesn't exist.");
	  System.exit(0);
	} catch (IOException e) {
	  System.out.println("I/O error");
	  System.exit(0);
	}
    return in;
  }
  
  private static void closeFile(BufferedReader in) {
    try {
	  in.close();
	} catch (IOException e) {
	  System.out.println("I/O error");
	  System.exit(0);	  
	}
  }

  private static PrintWriter openWriter(String filename) {
    try {
	  File file = new File(filename);
	  PrintWriter out =
	    new PrintWriter(
		  new BufferedWriter(
		    new FileWriter(file) ), true ); // (BufferedWriter) mode flush
			// new FileWriter(file,true) ) ); // (FileWriter) append mode
	  return out;
	} catch (IOException e) {
	  System.out.println("I/O Exception.");
	  System.exit(0);
	}
    return null;
  }

  // For reading Binary Files
  private static DataInputStream getBinaryStream(String filename) {
	DataInputStream in = null;
	try {
		File file = new File(filename);
		in = new DataInputStream(
				new BufferedInputStream(
						new FileInputStream(file) ) );
	} catch (FileNotFoundException e) {
		System.out.println(filename+" doesn't exist.");
		System.exit(0);
	} catch (IOException e) {
		System.out.println("I/O error");
		System.exit(0);
	}
	return in;
  }
  
  // For writing Binary Files
  private static DataOutputStream openBinaryStream(String filename) {
	  DataOutputStream out = null;
	  try {
		  File file = new File(filename);
		  out = new DataOutputStream(
				  new BufferedOutputStream(
						  new FileOutputStream(file) ) ); 
		  return out;
	  } catch (IOException e) {
		  System.out.println("I/O Exception.");
		  System.exit(0);
	  }
	  return null;
  }  
}
Advertisements
This entry was posted in Coding and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s