Chapter 11 Data from files
When a Java program runs, the data it operates on has to be in the computer’s memory. When we create a variable we are allocating a small bit of memory which we can then initialize to hold one value. When we create an array on an
ArrayList, we are allocating a larger chunk of memory that can hold lots of values. But where do any of those values come from?
They might come from our code. If we make an array like:
String[] foods = new String[] { "egg", "chicken", "fruit cup" };
we are creating an array that holds those three
String values.
But initializing values in our code this way is only reasonable when we’re dealing with small amounts of data. It would be really cumbersome to include a million element array of words in our source code. And if we did, it would mean that if we wanted to run the program on different data, we would need to change and recompile our program. So we need another way to load data if we want to processes a lot of data or process different data at different times with the same program.
Instead of embedding data directly in the source code of our programs, we typically store data in files on the computer’s hard drive and then load the data from those files into our programs. This allows us to write a program once that can run the same computation on different data just by loading a different file. (Programs can also write data to files but we are not going to cover that in this course.)
All of the different ways that data can be stored in files is a bigger topic than we can cover. Instead in this chapter we are going to focus on files that contain data sets, collections of data made up of many instances of the same basic kind of data such as a list words in a dictionary, of students in a class, or books in a library.

