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:
- Read the line from the file into a String variable
- Split the line into its parts by using the
split()
method on String - Instantiate a
CoffeeOrder
based on the parts that you read - Display the coffee order
- 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 newpublic static void main(String[] args)
method.- Leave the existing
App.java
alone.
- Leave the existing
-
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
'smain()
method and make sure it completes without an exception. -
If you get an exception (e.g.,
NullPointerException
), then you don't have theorders.csv
file in the right place.- Make sure the
orders.csv
is insrc/main/resources
inside your project folder.
- Make sure the
Step 3: Loop Through All Lines¶
Check to make sure you can read the file correctly:
-
Write a
while
loop that reads each line and then prints it out.while
Documentation for the while loop (and other loops) can be found here: https://books.trinket.io/thinkjava/chapter7.html
-
Make sure you check if the line is
null
, so you can use abreak
to exit the loop.readLine
JavaDoc for the readLine method are here: https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--
-
Run
FileApp
and make sure you see the contents of the file displayed and get no exceptions.
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.String.split JavaDoc
JavaDoc for this method is here: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-
-
For each string part, convert it to the appropriate
Enum
orString
, 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 theCoffeeOrder
)
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.