Skip to content

File Input/Output

In this lab you'll take coffee orders by reading them in from a comma-delimited file, e.g.:

small,milk,sugar
large,none,none
medium,half-n-half,splenda
medium,milk,none

Overview

Before diving into the details, let's take a high-level view of what you'll need to do. For each line in the file, you'll need to:

  1. Read the line from the file into a String variable
  2. Split the line into its parts by using the split() method on String
  3. Instantiate a CoffeeOrder based on the parts that you read
  4. Display the coffee order
  5. Loop until you've reached the end of the file

Step 1: Create the File on Your Machine

Download this file and copy it into the resources directory underneath your /src/main/ directory. Note: you might need to create the resources directory first.

Step 2: Create a New Main Class

  • Create a new Java class, FileApp, with a new public static void main(String[] args) method.

    • Leave the existing App.java alone.
  • Put the following code into the main method of the new class:

    String fileName = "orders.csv";
    try {
      URL file = FileApp.class.getResource(fileName);
      BufferedReader reader =
          Files.newBufferedReader(Paths.get(file.toURI()));
    } catch (IOException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
    
  • Run FileApp's main() method and make sure it completes without an exception.

  • If you get an exception (e.g., NullPointerException), then you don't have the orders.csv file in the right place.

    • Make sure the orders.csv is in src/main/resources inside your project folder.

Step 3: Loop Through All Lines

Check to make sure you can read the file correctly:

How might you write this as a test?

Instead of relying on watching the code output the file contents to the console, how might you write this in a testable way?


Step 4: Break Apart Each Line

Try to write test-first

Try to figure out how to write the test for each step below, instead of just heads-down writing code.

Now that you can read lines one at a time, let's look closer at what each line looks like:

small,milk,sugar

This is a comma-separated set of instructions of what coffee order to create. The above example would create a Small coffee, with Milk, and Sugar.

  • Using the String.split() method, break apart each line that you read into its 3 parts.

  • For each string part, convert it to the appropriate Enum or String, as necessary

    • This is similar to how you handled input from the user in a prior lab.
  • Instantiate a CoffeeOrder from the parts that you split

    • Use array access for what you got from String.split(), e.g.:

      String[] parts = line.split(","); // comma-separated
      String size = parts[0];
      String creamer = parts[1];
      String sweetener = parts[2];
      
  • Display the order (using display() on the CoffeeOrder)

Step 5: Try it!

Once you've completed the above steps, try it out by running the FileApp's main method and it should all work with no exceptions.


References

Chapter 10 of the Effective Java book is about how to do exception handling well.


Once you've completed the above steps, check in with the instructor to review your code.