Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 4th, 2006, 11:53 PM   #1
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 6th, 2006, 12:12 AM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
What, exactly, is giving you problems?
jaeusm is offline   Reply With Quote
Old May 6th, 2006, 12:37 AM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 6th, 2006, 1:57 AM   #4
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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"));
Now you can use the PrintWriter methods to write text to the file named "filename.txt":
pw.println("This is my file");
pw.flush();
When you are finished using the stream, be sure to close it:
pw.close();
Similarly, you can create a stream for reading data from a file. For instance, you might want to read in a line of text from a text file using a BufferedReader object:
BufferedReader br = new BufferedReader(new FileReader(new File("filename.txt")));
String line = br.readLine();
As mentioned above, when you are finished with the stream, make sure you close it.

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.
jaeusm is offline   Reply With Quote
Old May 6th, 2006, 8:51 AM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 6th, 2006, 11:03 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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();
Arevos is offline   Reply With Quote
Old May 6th, 2006, 11:14 AM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC