• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Java I/O

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java and presently going through I/O chapter. However, I am totally at loss and confused. There are so many classes and methods. Could some of you can advice what to learn about I/O and relevant classes and methods? Moreover, I have noticed that Sun Java programmer certification objective does not cover I/O. Am I correct?
I would really appreciate your help!
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you want to start to get good at this fancy I/O stuff, then I'd suggest a good reading of Sun's Java Tutorial on the subject, I/O: Reading and Writing (but no 'rithmetic), and that you spend some time over in the I/O forum, where I'm moving this thread to, now.
 
Jit Gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kathy and Dirk. Actually, I am reading from a book(Java2 A beginner's(?) guide), where there are too many lessons on how to read binary data and also byte data stream. I was confused.
Once again thanks very much!
 
Ranch Hand
Posts: 8943
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new java nio API allows us read /write in blocks rather a single byte.
What about BufferedReader? Does it buffer bytes by reading them one by one?
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly Jit, I wouldn't try wrapping your head around reading and writing bytes until the time that you find you need to. There are so many other classes that allow you to deal with data in a format that makes sense, that at least in my experience it is very rare to need to dig down to the byte level. Take Kathy and Dirk's advice, focus on the simpler I/O and then you'll find the rest pretty much has the same mannerisms.
While I'm not a fan of some of Sun's tutorials, their I/O one is one of the best out there!
 
    Bookmark Topic Watch Topic
  • New Topic