Docsity

Input and Output in Java: Reading and Writing Files, Study notes of Computer Science

Toggle sidebar
bg1
1 Input, Output, Formatting, Files
One of the most painful parts of any programming experience is always that of input and output. This is
painful only because in doing I/O one has to interact with the operating system and the file system, and thus
one has to interact with things outside the sphere of one’s own particular program in execution in memory.
Since the OS and the files change over time, one has to be very careful if one is going to write code that runs
correctly. Also, in interacting with a human user, one has to bulletproof the input so that no matter how
unusual the input is, one can correctly determine if it makes sense.
The simplest way to do input from the console, and the one we will use pretty much exclusively in this
course, is the Scanner class. A standard use of this class is shown in Main1 on the website, and is shown
below. A nice thing about the Scanner class is that one can test for the presence of a data input of the
correct type rather than write error-handling code. This is simply easier, and it is highly recommended.
The simplest way to do output to the console is with the System.out.println(); statement. This in
essence causes the variables, regardless of type, to be converted into strings of a nature appropriate for printing
out (integers for integers, numbers with decimal points for doubles, etc.). When one is simply debugging code,
the println may be the easiest way to get debugging output printed.
CSCE 145, Fall 2007 Notes 2 Page 1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Input and Output in Java: Reading and Writing Files and more Study notes Computer Science in PDF only on Docsity!

1 Input, Output, Formatting, Files

One of the most painful parts of any programming experience is always that of input and output. This is

painful only because in doing I/O one has to interact with the operating system and the file system, and thus

one has to interact with things outside the sphere of one’s own particular program in execution in memory.

Since the OS and the files change over time, one has to be very careful if one is going to write code that runs

correctly. Also, in interacting with a human user, one has to bulletproof the input so that no matter how

unusual the input is, one can correctly determine if it makes sense.

The simplest way to do input from the console, and the one we will use pretty much exclusively in this

course, is the Scanner class. A standard use of this class is shown in Main1 on the website, and is shown

below. A nice thing about the Scanner class is that one can test for the presence of a data input of the

correct type rather than write error-handling code. This is simply easier, and it is highly recommended.

The simplest way to do output to the console is with the System.out.println(); statement. This in

essence causes the variables, regardless of type, to be converted into strings of a nature appropriate for printing

out (integers for integers, numbers with decimal points for doubles, etc.). When one is simply debugging code,

the println may be the easiest way to get debugging output printed.

When one needs more formal output, however, the System.out.printf(); command is much to be

preferred. This is a language construct that is part of Java 5.0 and higher, but is not part of Java 1.4,

so if you wind up using an early version of Java you will get compiler errors. The syntax for this is

System.out.printf("format string",variable names separated by commas); The format string

needs to have formatting information that lines up in type and number with the type and number of the

variables whose names appear separated by commas. The format string can also have text included, and

blanks included in the format string are significant in that they cause blank spaces to be produced in the

output.

The basic formatting rules include the following

• %d, for integers printed with however many spaces are needed

• %10d, for integers printed in a field exactly ten spaces wide

• %12d, for integers printed in a field exactly 12 spaces wide

• %f, for doubles printed with however many spaces are needed

• %10.2f for doubles printed with a 10-space field and two decimal places

• %24.20f for doubles printed with a 24-space field and 20 decimal places

2 Reading From a File

Reading from the console is one thing, but sometimes what you have to do is read data from a file. One

example of how to do this is shown in Main2, shown below and on the web.

try

dataFile = new Scanner(new File("theDataFile.dat"));

catch (FileNotFoundException ex)

System.out.println("ERROR opening inFile " + "theDataFile.dat");

System.out.println(ex.getMessage());

System.out.println("in" + System.getProperty("user.dir"));

System.exit(1);

There’s a lot of boilerplate language stuff in this sample program. The try-catch is the Java construct

that allows you to try to execute a line of code, but if a runtime error occurs, then you can catch the error

and process it. In this particular example, you try to open a data file of a given filename, but if that file

doesn’t exist, you print an error message and exit.

The third version of how to read data, and the second version of how to read from a file, uses static class

variables and illustrates how you can clean up your programs by pushing off the error checking into a utilities

java file.

The Utilities.java program has two static methods. One of these is the ScannerOpen code from

Main2. This is now a public static method, and what public static means is that you can invoke

this method as a class method with the Utilities.ScannerOpen(); command. (The class method part is

what allows you to use Utilities as the first part of the invocation of that command.)

Note carefully in both Main2 and in Main3 and Utilities what imported files have been requested.

These are the java files that include the exception handling code, etc. You will need these.

Note also the difference between the files as produced by linux and by a Windows machine. It is a

characteristic of Windows that text files and data files that a given “line” of text ends in a carriage return

(ASCII 13 decimal) and a line feed (ASCII 10 decimal). Linux, however, uses only the line feed.

For example, the output of the Main3 program in the InputOutput2 example is, under linux, the bytes

0000000 f i l e sp v e r s i o n sp s t r 0000020 i n g sp i s sp 0 9 / 0 1 / 2 0 0 0000040 7 nl f i l e sp v e r s i o n sp s 0000060 t r i n g sp i s sp 5 0 0. 0 0 nl 0000100 f i l e sp v e r s i o n sp s t r 0000120 i n g sp i s sp b a l a n c e nl f 0000140 i l e sp v e r s i o n sp s t r i 0000160 n g sp i s sp 0 9 / 1 0 / 2 0 0 7 0000200 nl f i l e sp v e r s i o n sp s t 0000220 r i n g sp i s sp - 2 5. 5 7 nl f 0000240 i l e sp v e r s i o n sp s t r i 0000260 n g sp i s sp g r o c e r i e s nl 0000300 f i l e sp v e r s i o n sp s t r 0000320 i n g sp i s sp 0 9 / 1 2 / 2 0 0 0000340 7 nl f i l e sp v e r s i o n sp s 0000360 t r i n g sp i s sp - 1 1 8. 1 9 0000400 nl f i l e sp v e r s i o n sp s t 0000420 r i n g sp i s sp e l e c t r i c 0000440 i t y nl f i l e sp v e r s i o n 0000460 sp s t r i n g sp i s sp 0 9 / 1 3 0000500 / 2 0 0 7 nl f i l e sp v e r s i 0000520 o n sp s t r i n g sp i s sp 1 0 0

  1. 0 0 nl f i l e sp v e r s i o n

0000560 sp s t r i n g sp i s sp b i r t h 0000600 d a y nl f i l e sp v e r s i o n 0000620 sp s t r i n g sp i s sp 0 9 / 1 4 0000640 / 2 0 0 7 nl f i l e sp v e r s i 0000660 o n sp s t r i n g sp i s sp - 7 3

  1. 0 2 nl f i l e sp v e r s i o n 0000720 sp s t r i n g sp i s sp p r e s e 0000740 n t s nl

0000560 i l e sp v e r s i o n sp s t r i 0000600 n g sp i s sp b i r t h d a y cr nl 0000620 f i l e sp v e r s i o n sp s t r 0000640 i n g sp i s sp 0 9 / 1 4 / 2 0 0 0000660 7 cr nl f i l e sp v e r s i o n sp 0000700 s t r i n g sp i s sp - 7 3. 0 2 0000720 cr nl f i l e sp v e r s i o n sp s 0000740 t r i n g sp i s sp p r e s e n t 0000760 s cr nl