Howdy! Well, you're right on all counts... there are a gazillion I/O classes (even not counting the NIO classes which -- if you're new -- you shouldn't worry about now.) And you're also right that there are *no* I/O classes on the 1.4 programmer's exam.
The good news about I/O is that you can learn just a few classes and do 90% of what you'd need to do. Most of the rest are so that you can mix and match and get whatever you might need by combining classes together.
So, which are those *few*? Well, everyone has their own, but I'll give you a couple to start with for files:
To read text from a file:
=================
File myFile = new File("MyWonderfulText.txt");
// we've got a reference to a File, but now we want to get at the content IN the file
FileReader fileReader = new FileReader(myFile);
// that will do, but it'll be more efficient if we use a buffer so that we can read a bunch of stuff in at a time
BufferedReader reader = new BufferedReader(fileReader);
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// then don't forget to close! You only have to close the top of the stream -- the thing you're actually calling methods on, and it will close all of the others you 'chained' to it.
reader.close();
// and don't forget that EVERYTHING needs to be in a try/catch.
==============
To WRITE to a String to a text file (it's almost the opposite)
FileWriter writer = new FileWriter("Foo.txt");
writer.write("Hello foo!");
writer.close();
Or, chain a buffered writer onto the end of a file writer (which is linked directly to your file...)
BufferedWriter buffWrite = new BufferedWriter(new FileWriter(aFile));
buffWrite.write(....)
====================
Here are a few you might want to look at:
FileWriter
BufferedWriter
FileReader
BufferedReader
File
Or, if you do object serialization:
ObjectInputStream
ObjectOutputStream
One cool thing about java networking is that it's just like the I/O you've been using...
(don't worry about the networking here; this is just to point out how I/O-ish it is)
Socket s = new Socket("127.0.0.1", 4242);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
reader.close();
cheers,
Kathy
