aspose file tools*
The moose likes Beginning Java and the fly likes PrintWriter constructor I/O exception 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 "PrintWriter constructor I/O exception" Watch "PrintWriter constructor I/O exception" New topic
Author

PrintWriter constructor I/O exception

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

Hello Experts,

Just wondering, why when you invoke a constructor of the PrintWriter class, it is possible that an I/O exception will be thrown ?

Because the app can not create a new file on the disk, or it does not have the permissions to write a file, in case it already exists ?

Please be specific, and go easy on me, I'm a Java rookie.

Thank you so much for your help !
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11479
    
  94

A great way to work this out is to look at the JavaDoc for the class constructor. If you look at where you typed PrintWriter in your post, you will see that it is underlined - it automatically got converted to a link to the JavaDoc.

Looking at the details for public PrintWriter(File file), I can see two exceptions can be thrown based on your criteria.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Also, note that IOException and its subclasses are checked exceptions - which means that if that exception can be thrown by a given constructor or method, it will be shown in the javadoc. And if it can't, it won't. So for any constructors you see that do not say "throws IOException", that means they don't.
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

well,

besides Microsoft's documentation, which is legendary for being totally useless, Javadoc si not that good either, because the lack of relevant examples for methods for eg, a good deal of ambiguity and too abstract for greenhorns, so it's not my favorite place to go.

Here is the definition taken from Javadoc :

" FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
"

1. What do they mean by regular file exactly, what extension the file must have ?
2. what do they mean by operating the file ?
3. what is a security manager ?

4. all these long, ambiguous, do-3-searches-to-get-an-idea lines, couldn't be reduced to :

a) the file does not exists at the specified location ,
b) the file is read-only for the user that tries to access it. ?

Thank you very much for all your help !

Kind regards,
Marius
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Marius Constantin wrote:1. What do they mean by regular file exactly, what extension the file must have ?
2. what do they mean by operating the file ?
3. what is a security manager ?

4. all these long, ambiguous, do-3-searches-to-get-an-idea lines, couldn't be reduced to :

a) the file does not exists at the specified location ,
b) the file is read-only for the user that tries to access it. ?


1. Some operating systems have files which are reserved for use by the operating system only, and not accessible to users. Since Java can be run on many operating systems, including ones which haven't been written yet, it would be impractical to include a large section which explains exactly what "regular" means on a long list of operating systems.

And many operating systems don't even have the concept of "file extension" so there is no reason for the Java API to get involved with that.

2. I don't know... did they really use that phrase? I don't see it in the API docs for java.io.File.

3. You could look that up. The API documentation is for documenting how classes and methods work. It isn't to be confused with a tutorial.

4. No, because different operating systems have a variety of ways to permit users to access files. Some operating systems have only a read-only flag, others have a much more complicated set of permissions.

In other words, their world is more complicated than your world is. You can't expect them to produce a simplified version of the API which caters to your world; it has to cover all of the possibilities.
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

Paul Clapham wrote:
1. Some operating systems have files which are reserved for use by the operating system only, and not accessible to users. Since Java can be run on many operating systems, including ones which haven't been written yet, it would be impractical to include a large section which explains exactly what "regular" means on a long list of operating systems.

And many operating systems don't even have the concept of "file extension" so there is no reason for the Java API to get involved with that.

2. I don't know... did they really use that phrase? I don't see it in the API docs for java.io.File.

3. You could look that up. The API documentation is for documenting how classes and methods work. It isn't to be confused with a tutorial.

4. No, because different operating systems have a variety of ways to permit users to access files. Some operating systems have only a read-only flag, others have a much more complicated set of permissions.

In other words, their world is more complicated than your world is. You can't expect them to produce a simplified version of the API which caters to your world; it has to cover all of the possibilities.


1. They could at least narrow down the ambiguity to windows, unix based OS.s, and OS X.

2. please see attached file for PrintWriter instantiation

4. but since every major OS is based on user sessions and user permissions, you can say for short that a given file is read only for a certain user, not telling exactly how this is done. I think this is the point.

in other words... part ) You can really create a language doc, for eg http://api.rubyonrails.org/ , not just create one for the sake of ... I really can' t find a reason.

Thank you for your time.



[Thumbnail for Screenshot from 2012-07-23 22:26:21.png]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PrintWriter constructor I/O exception