Reading data from files
A common task we'll need to perform as programmers is reading input from a file. In this section, we're going to take a quick look at how to acquire text input from files.
Note
We've gone ahead and told Java that sometimes our main
method will simply throw IOException
errors. Both the FileWriter
and FileReader
objects in the following code block can create a number of IOException
errors for a number of reasons, for example, if they can't connect to the files they're supposed to.
package inputandoutput; import java.io.*; public class InputAndOutput { public static void main(String[] args) throws IOException { File outFile = new File("OutputFile.txt"); File inFile = new File("InputFile.txt"); FileWriter out = new FileWriter(outFile); FileReader in = new FileReader(inFile); //Code Here... out.close(); in.close(); } }
When writing actual programs for actual applications...