Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a PrintWriter object in Windows.  RSS feed

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a PrintWriter object.
The first approach below is okay but not the second one.
Could someone please tell me what I'm doing wrong?

First approach:


Second approach:
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the file "C:\myprojects\12345.xml" exist?
What is printed when you print new File("12345.xml").getAbsolutePath()?
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Does the file "C:\myprojects\12345.xml" exist?
No, but that's what I'm actually wondering. If I don "12345.xml" it will create the file if it doesn't exist. Why not in this case?

> What is printed when you print new File("12345.xml").getAbsolutePath()?
C:\myprojects\
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Susan]: The first approach below is okay but not the second one.

What exactly is wrong with the second? Does it throw an exception? If so, what's the error message? Or does somethign else go wrong?

[Susan]: No, but that's what I'm actually wondering. If I don "12345.xml" it will create the file if it doesn't exist. Why not in this case?

Well, maybe the directory doesn't exist. Or maybe the directory exists but you don't have write access. If the file already exists, then it coudl be that you don't have access to overwrite it. Or maybe there's a stream open somewhere writing to the file or reading from it.

[Susan]: > What is printed when you print new File("12345.xml").getAbsolutePath()?
C:\myprojects\


Um, really? With no 12345.xml at the end? That's extremely weird. You sure about that?
[ December 04, 2007: Message edited by: Jim Yingst ]
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> What's wrong with the second one?
File Not Found Exception

>> Um, really? With no 12345.xml at the end? That's extremely weird.
>> You sure about that?
Ops, you're right.
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API, about when FileNotFoundException is thrown:
if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

The first case does not seem to be the problem, but have you tested the second one? Can you write other files to that directory?

And can you show us the stack trace?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!