files - File I/O This section will describe how to read...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
File I/O This section will describe how to read from a text file and how to write to a text file. In both cases, you will need to include the line: import java.io.*; You will also need to add: throws IOException to the end of the main method. Thus, the main method will look like this: public static void main(String[] args) throws IOException { } This means that interacting with files can cause some unexpected problems (like if the file isn’t there), but that we’re not going to handle them. Later in the course we will discuss how to handle these problems. Reading from a File First, we need to open a connection to the file. We do this by creating a Scanner (like we do to get user input). This time, however, we’ll connect the Scanner to a file: Scanner s = new Scanner(new File(filename)); Here, filename is a string that is the name of the input file. This file should be stored in the same directory as the class that’s using it. Alternatively, you can specify the absolute path of the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 3

files - File I/O This section will describe how to read...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online