File formats
The Countries.dat
data file in the preceding example is a flat file—an ordinary text file with no special structure or formatting. It is the simplest kind of data file.
Another simple, common format for data files is the comma separated values (CSV) file. It is also a text file, but uses commas instead of blanks to separate the data values. Here is the same data as before, in CSV format:

Figure 2-4 A CSV data file
Note
In this example, we have added a header line that identifies the columns by name: Country
and Population
.
For Java to process this correctly, we must tell the Scanner
object to use the comma as a delimiter. This is done at line 18, right after the input
object is instantiated:

Listing 2-3 A program for reading CSV data
The regular expression ,|\\s
means comma or any white space. The Java symbol for white space (blanks, tabs, newline, and so on.) is denoted by '\s'
. When used in a string, the backslash character itself must be escaped with another preceding backslash...