This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Input Output Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Input Output Questions" Watch "Input Output Questions" New topic
Author

Input Output Questions

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
I have got some queries related to java Io System.I'll be OBLIGED if u answer them for me.
1.When does FileOutputStream throw a FileNotFoundException?
2.Why do we need byte array input stream?Cant't me directly read from a byte array?
3.Is FilterOutputStream abstract?
4.What happens when we call the mark method on a BufferedInputStream object with an argument greater than its buffer size?
I tried it but it did not throw any exception.But I read in some books that this throws Exceptions.
5.Can any 1 explain me the Unread method of PushbackInputStream class?
6.Why does SequenceInputStream ask for a Enumeration in its constructor,I think it has been deprecated,so I dont know how to use it.
7.Why do we need PrintStream,when we have got DataOutputStream?
An unrelated question:
1.How can we declare main() method with private accesibility and still compile and run the program?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Gautam Sewani:
1.When does FileOutputStream throw a FileNotFoundException?


Check the API Spec

2.Why do we need byte array input stream?Cant't me directly read from a byte array?

The ByteArrayInputStream class contains an internal byte array which it can "buffer" from. Again, check the API Spec for more details.

3.Is FilterOutputStream abstract?

Check the API Spec. The answer is no.

4.What happens when we call the mark method on a BufferedInputStream object with an argument greater than its buffer size?

Check the API Spec.

5.Can any 1 explain me the Unread method of PushbackInputStream class?

Check the API Spec.

6.Why does SequenceInputStream ask for a Enumeration in its constructor,I think it has been deprecated,so I dont know how to use it.

The functionality of Enumeration has been duplicated by Iterator, but I don't believe this interface is deprecated. Someone please correct me if I'm wrong. To see how to use it, check the SPI Spec.

7.Why do we need PrintStream,when we have got DataOutputStream?

Read the summary for each class in the API Spec for DataOutputStream and PrintStream. The purpose of PrintStream is explained.

1.How can we declare main() method with private accesibility and still compile and run the program?

I believe this is a bug in the compiler. According to the JLS, the main method (when used as an entry point), must be public, static, and void, and take an array of Strings as a parameter.
Corey


SCJP Tipline, etc.
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
Thanks for the api specs
but I wanted answers from u!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
answers... answers... answers. But what about good questions?

The link given by Corey provides the answer. :roll:

2.Why do we need byte array input stream?Cant't me directly read from a byte array?

Yes we can, but not using the same interface as with the rest of input streams. That is, you can feed a program that needs an input stream with a byte array thanks to ByteArrayInputStream.

4.What happens when we call the mark method on a BufferedInputStream object with an argument greater than its buffer size?
I tried it but it did not throw any exception.But I read in some books that this throws Exceptions.

No exception is thrown. A new buffer with a proper size is created to manage such mark.
The IOException will be thrown by seset only if either :
a) it was called without having invoked previously to mark
b) it was called having read more bytes, from the last mark, than specified by the argument to mark.
This code shows it:


5.Can any 1 explain me the Unread method of PushbackInputStream class?

URL given by Corey gives the answer. This is used generally by parsers.
I back up the rest of the answers given by Corey.
[ May 12, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Gautam Sewani:
Thanks for the api specs
but I wanted answers from u!

Corey's trying to give you an invaluable tool -- the ability to use outside resources. You'll get a much quicker, more thorough answer if you know WHERE to look for it -- rather than just posting a question, waiting for someone else to answer.... Not to say you shouldn't post questions -- but definatley check out the links he provided, you'll be able to learn so much for yourself!
I've got copies of 3 or 4 different specs and about 10 API's that I consult many times a week. A VERY helpful resource.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Gautam Sewani:
Thanks for the api specs
but I wanted answers from u!

Guatam, I wasn't trying to blow you off (well, I kinda was, but that wasn't the point). I was trying to direct you to another source of information to use - the Java API Specification. Make a bookmark to it. If you're going to do any sort of Java development (and, since you're after your SCJP, I'll assume you're going to), you'll use it repeatedly.
Virtually every question you asked is answered within the API Spec. If you look in there, you'll find descriptions for every class and every public method within those classes, including the parameters accepted, return types, and exceptions thrown. These are all things that you'll need to be able to find readily when you're doing Java development.
I don't want to discourage you from posting questions here, but you should consult your resources prior to doing so. When I come up with a question, I have a few places I check. I can check the API Specification, I can check the JLS or JVMS, or I can do a search in this forum. If I can't find an answer, then I ask a question here.
At first, you might find this to be a slow process but one of the goals I have is to teach you how to learn Java, not just to teach Java. Soon, you'll get to know how to use tools like the API Spec and the JLS and you'll be able to find your answers much faster (and be more sure of the answers) than coming to JavaRanch and waiting for a reply.
As I said, I don't want to discourage you (or anyone) from using JavaRanch - I think it's an incredible resource, but there are other tools out there as well and the more of them that you can use, the more effective you'll become.
Corey
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Corey McGlone:
At first, you might find this to be a slow process but one of the goals I have is to teach you how to learn Java, not just to teach Java.
There was a 6 hour space between Guatam's first post and the first answer. In that space he could have answered all his own questions by using the API. Anyone wanting to get certified must be able to read and understand the API.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I must admit that if you are beginning you are not able to understand neither API or JLS. The understanding is a gradual and painful process. But also rewarding.
By giving these resources we expect at least a try, an effort to read them. The reason has been pointed above, nobody can become A SCJP if he/she is not able to read fluently some parts of the API/JLS.
If anybody has already made this effort we are happy to help. (We are volunteers, no staff paid here)
There is also another way to learn. Writting code. Many questions can be anwered by writtin code first. Again, we are happy to help if that code doesn't work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Input Output Questions