![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Sexy Programmer
|
Java's I/O Files and Streams
I am now touching on Java I/O Files and Stream classes. I have read 3 different books on I/O classes in Java. All of these books are so consistent up to the subject of files and streams.
Does anyone have any good advice for making files and streams a lot easier to learn and comprehend. Or maybe another book which taught you Java's Files and Stream classes well. I don't know if it's me or the books (I know it's me...) but I just don't seem to get this subject.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
What, exactly, is giving you problems?
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
The whole I/O classes. Just an easier way to learn how to open files, save files, and read files, and use file data for my programs.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Conceptually, you can think of streams as the medium over which data is transmitted between two "things" -- in this case, your program and a file. In other words, you need to:
1. Create a stream for either input or output, 2. Connect one end to your program, 3. Connect the other end to a file. These steps are only conceptual, as you can achieve all this in one line of code. For instance, in order to write data to file, you might use a PrintWriter object: PrintWriter pw = new PrintWriter(new File("filename.txt"));pw.println("This is my file");
pw.flush();pw.close(); BufferedReader br = new BufferedReader(new FileReader(new File("filename.txt")));
String line = br.readLine();There are several different stream classes you can choose from. BufferedReader is a good choice when working with text files. If you were working with files that did not contain readable text, another stream class such as InputStream might be a good choice. Each of the stream classes have their strengths, weaknesses, and applications. |
|
|
|
|
|
#5 |
|
Sexy Programmer
|
How would I be able to retreive objects and the data that I created in my program from a file. Say example, I have a 2 textfields and the user put his phone number in one and last name in another, how would I be able to retreive the data from which a user entered in and have it exactly how it was.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
You can use an ObjectOutputStream object to save objects that can be serialized. An object needs to first implement the Serializable interface in order to be serialized (most basic Java types do by default).
// Open an output stream to a file
OutputStream outputStream = new FileOutputStream("filename.txt");
// Layer an ObjectOutputStream on top of the FileOutputStream
ObjectOutputStream objectOutput = new ObjectOutputStream(outputStream);
// Save an object
objectOutput.writeObject("An object, in this case, a normal String");
// Close the stream
objectOutput.close();
// Open an input stream to a file
InputStream inputStream = new FileInputStream("filename.txt");
// Layer an ObjectInputStream on top of the FileInputStream
ObjectInputStream objectInput = new ObjectInputStream(inputStream);
// Load an object
String myMessage = (String)objectInput.readObject();
// Close the stream
objectInput.close(); |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
As an aside, Java streams make quite a bit of sense after a while of using them. A stream in Java is a way to output a series of bytes, or to receive a series of bytes as input. This is a very low level approach, and normally programmers don't directly access streams. Often, functionality is layered on top of these streams with extra classes.
For instance, the PrintWriter object allows you to send strings directly into the stream using the print and println methods. System.out is itself a PrintWriter object. Another example is the ObjectOutputStream shown above. This object allows a user to send objects across a stream. And it's not just files that use streams. Any continuous data connection in Java does including TCP connections over the Internet. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|