File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating a PrintWriter object in Windows. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Creating a PrintWriter object in Windows." Watch "Creating a PrintWriter object in Windows." New topic
Author

Creating a PrintWriter object in Windows.

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 223
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

Joined: Oct 27, 2005
Posts: 19232

Does the file "C:\myprojects\12345.xml" exist?
What is printed when you print new File("12345.xml").getAbsolutePath()?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 223
> 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

Joined: Jan 30, 2000
Posts: 18670
[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 ]

"I'm not back." - Bill Harding, Twister
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 223
>> 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

Joined: Oct 27, 2005
Posts: 19232

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a PrintWriter object in Windows.
 
Similar Threads
Sequence of proper wrapping selection(File I/O doubt)
PrintWriter not writing twice?
PrintWriter vs FileWriter
Writing an Empty Line in a File....
Creating and Writing a file