aspose file tools*
The moose likes Beginning Java and the fly likes About printing and reading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About printing and reading" Watch "About printing and reading" New topic
Author

About printing and reading

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Hi guys, so whenever I program, I always experience problems like reading a file as input and output the result to another file, or to the screen. So I just have some questions regarding the different java classes that I have used so far, and what their differences are. I also gave some examples.

1. BufferedReader, FileReader, InputStreamReader. The following are some applications:

Its parameter is: new FileReader. So why we need FileReader? What is its function? Why can't we just have: ... = new BufferedReader("personDataBase")?

So here we have InputStreamReader in the parameter, what's its difference from FileReader?

2. So is FileWriter correspondent with FileReader? BufferedWriter is correspondent with BufferedReader?
ex.
How is FileWriter different from BufferedWriter, and vice versa?

3. File class. So when I work on unix, and I want to feed a file to a class, I type: java HelloWorld greeting.txt. That's when we need File class:

So the File class here simply just creates an instance of this file? why can't we use FileReader or something like above to directly read it?

4. What are PrintStream class and PrintWriter class?


Thanks for all the clarifications!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

So why we need FileReader? What is its function? Why can't we just have: ... = new BufferedReader("personDataBase")?

When you bake a cake, do the directions say "plant some grain, wait 6 months, harvest it, grind the seeds into a white powder. You will need two cups of that powder" or does it say "get two cups of flour"?

The idea here is that each of these objects do a little something, and you build off what others have done. And sometimes, you may what whole wheat flour, sometimes cake flower, sometimes all purpose flour...there may be times when you would want to pass something other than a FileReader into a BufferedReader.

How is FileWriter different from BufferedWriter, and vice versa?

Did you check the API?
when I work on unix, and I want to feed a file to a class, I type: java HelloWorld greeting.txt

This does not feed the file into java. It feeds in a string of "greeting.txt". You could pass in ANY string this way: "Fred", "shoo-be-do-be-dooo" or "this is one kind of long string for a java argument". you can pass in as many strings as you want, separated by spaces. What you do with that string can vary in the program. So, the File class is used to take the string given, treat it as the name of a file, and open a handle to it.
What are PrintStream class and PrintWriter class?

Again, check the API, linked above.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
fred rosenberger wrote:
So why we need FileReader? What is its function? Why can't we just have: ... = new BufferedReader("personDataBase")?

When you bake a cake, do the directions say "plant some grain, wait 6 months, harvest it, grind the seeds into a white powder. You will need two cups of that powder" or does it say "get two cups of flour"?

The idea here is that each of these objects do a little something, and you build off what others have done. And sometimes, you may what whole wheat flour, sometimes cake flower, sometimes all purpose flour...there may be times when you would want to pass something other than a FileReader into a BufferedReader.


Hi fred, thanks for some brief explanation. However, to be totally honest, I did check API for all of these before I posted these questions, but I still couldn't figure out the differences.
Let's now look at FileReader and BufferedReader:
"FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream."
BufferedReader: "Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines."
I still can't tell the difference between the two. And what does it mean by "raw bytes, using FileInputStream"?
I just need someone to give me some examples of each, so I get an idea of when to use them. Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
There should be lots in the Java™ Tutorials; look for the "buffered" subsection.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Campbell Ritchie wrote:There should be lots in the Java™ Tutorials; look for the "buffered" subsection.


Thanks, Campbell. The link was very useful. However, it didn't explain FileReader. I use it only when I need to use BufferedReader. So I am wondering if it has other uses, or like what's the difference between FileReader and BufferedReader?
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
One way of looking at the differences is in steps. With FileReader you read what? Just the characters, right? so what do you read should you have something other than simple characters? What if you tried storing multiple lines, serialized objects, or arrays? The stored information would simply read as a sentence for the FileReader.

BufferedReader, on the other hand, is more diverse in its capabilities. Using the information it receives from the FileReader it translates that information into meaningful code as well as Strings, should there be any, for you to then convert and manipulate in your program without adding a lot of additional, already been done for you, code.

In the above cake example, it is similar to building a box-mix cake and a cake from scratch with over 200 ingredients, some of which are scarcer and harder to find than others. Some of those ingredients can be used up by dumping in the box cake and then adding your applesauce-extender for that extra large cake!


Jason K. (AC)
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Jason Koonce wrote:One way of looking at the differences is in steps. With FileReader you read what? Just the characters, right? so what do you read should you have something other than simple characters? What if you tried storing multiple lines, serialized objects, or arrays? The stored information would simply read as a sentence for the FileReader.

BufferedReader, on the other hand, is more diverse in its capabilities. Using the information it receives from the FileReader it translates that information into meaningful code as well as Strings, should there be any, for you to then convert and manipulate in your program without adding a lot of additional, already been done for you, code.


Hi Jason, thanks for the explanation. So you were saying that for FileReader, it reads characters from a word, like "hello"? But I think it should also be capable of reading multiple lines since they are just consisted of "many" characters? Arrays are usually words or numbers as well. I am not so clear at what you were trying to say about FileReader? Were you saying that FileReader can read them all, but doesn't process them correctly/ translate them into meaningful code?
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
Good questions! Since I am still learning, this is actually challenging me to answer correctly, so in truth I hope I am giving good explanations.

The FileReader reads files, but to read code, as in objects, you need a BufferedReader. Ok, here is an example:

Let's say you have a string that says: "I have a long eared dog." That is a String of characters and spaces, right? FileReader can use that and pull it from a file for you to do with whatever you like.

BufferedReader, though, can play with serialized objects. Instead of the above string you now have an array of strings: String dog[] = [ "I ", "have ", "a ", "long ", "eared ", "dog." ]; Buffered reader can not only read the String portions of that array, but can actually pull the array out of your stored file for you to use and manipulate as if you had written it directly into your code. This allows you to manipulate files or use files in your code that are of similar type, but specific to different people/needs. A FileReader would have looked at each part as a String whereas the BufferedReader is a little more advanced. The funny thing is the parameters for the BufferedReader would be something like this:

BufferedReader read = new BufferedReader( FileReader( dog.txt ) );

Just because it is more basic in its capabilities doesn't mean it isn't just as important. Understanding their different roles and capabilities are very useful. I would suggest learning how to read the API's so that you can better understand how these two classes, and other classes work together. It will eventually make everything a lot easier for you, and it is definitely a learned skill to read the API's!

Good luck and I hope this clears it up a little!
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Hey Jason! Thanks a lot! This makes a lot of sense now. BufferedReader is more advanced compared to FileReader. I think if I just want to read a sting of characters, then FileReader would suffice. And you are absolutely right that it does take some skill to read API. Many people were like: just read the API. And I was like: I did try! but without much success. I think programming teachers do not really teach those IO stuff, so we have to look up everything on our own. One more question though, do you know when to use InputStreamReader? Because I have seen it being used in BufferedReader as well. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) THANKS!
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
Here is a lesson in both the InputStream and API, so sorry for the rough posting ahead of time!

from the API: <quote> "An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in)); " </quote>

Now, below that is a bunch of methods, class summaries and things like that. Basically, what this is, is a small description in semi-plain English about what the class does. In this case it takes byte code and translates it to characters for other readers to make use of. In other words, a serialized non-text file that was stored as byte-code.

Hope this helps!

Ok, so the methods contained in the class are methods you may or may not need to use. It just depends on what you need to do with the object or file that you are using. For instance. the close() method closes the file when your done with it preventing memory leaks and such.

Should you see a description that you like, but can't figure out how to implement the method, use Dr. Google by typing in the method and 'examples' after the method name. I also add 'Java' before the method to, so I usually try 'Java method/class example' to help me figure out how to implement, but only after reading the API so I actually learn it.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Jason Koonce wrote:;

Now, below that is a bunch of methods, class summaries and things like that. Basically, what this is, is a small description in semi-plain English about what the class does. In this case it takes byte code and translates it to characters for other readers to make use of. In other words, a serialized non-text file that was stored as byte-code.


Hey Jason! Thanks as always. I am just wondering what does a byte-code looks like? Because I feel like so far I have been using text file that contains strings. But I did use InputStreamReader once, but I don't remember why?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
javac Foo.java
javap -c Foo
javap Foo
. . . and see the output with and without -c.
You can also open a .class file with a hex editor, and find the list of codes in the Virtual Machine Specification.
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
Cheryl Scodario wrote:Hey Jason! Thanks as always. I am just wondering what does a byte-code looks like? Because I feel like so far I have been using text file that contains strings. But I did use InputStreamReader once, but I don't remember why?


To be honest, I don't know. I was told it was something to do with how you save your file. My instructor said that if you saved it as .ser instead of .txt with a serialized method. I opened up the file in notepad just to see and it had all of the letters and numbers I typed plus a lot more stuff that looked like it was code-shorthand for the object. But none of it really made much sense.

All I can say is, when deciding how to save your file, in a non-school environment (school should always tell you what they want from you) and your employer/task manager for any project you are working on doesn't tell you what to save it as, look at the program requirements, how the information was previously handled and see what works best. Of course, I know this place that might help too. Lots of great coders get together to give people like me and you advice. It's called The Java Ranch!

Good luck!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About printing and reading