• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PrintWriter constructor I/O exception

 
Ranch Hand
Posts: 62
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 4830
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 62
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Screenshot-from-2012-07-23-22-26-21.png
[Thumbnail for Screenshot-from-2012-07-23-22-26-21.png]
PrintWriter from Javadoc
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic