File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FileInputStream creating a new File ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FileInputStream creating a new File ??" Watch "FileInputStream creating a new File ??" New topic
Author

FileInputStream creating a new File ??

Sudhansu Pati
Ranch Hand

Joined: Sep 23, 2003
Posts: 77
Hello Gurus,
Got stuck with a silly issue. FileInputStream is creating a new file, if the file doesn't exist. I don't want the FileInputStream to create a new file for me. I would rather expect it to throw the FileNotFoundException.

I read Sun's documentation and the documentation says, FileInputStream trys to read the file, but if the file is not found, then FileNotFoundException is thrown.

The following code always create a new file, if it doesn't already exist and never throws FileNotFoundException.
Can't believe that Sun's documentation is misleading. Your help will be much appreciated.

[ November 13, 2003: Message edited by: Suds Pati ]

Sudhansu<br />SCJP, SCWCD, SCBCD, SCJD, SCEA, SCDJWS
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
To be honest, that sounds pretty unlikely. I suspect that the file really did exist before you tried to read it. Maybe you've got some other code somewhere which creates it? Try adding some debug code:


"I'm not back." - Bill Harding, Twister
Sudhansu Pati
Ranch Hand

Joined: Sep 23, 2003
Posts: 77
Hi Jim,
Thank you for your quick response. What can I say. I am puling my hair all day long. I have checked with your code too.

File f = new File(path) also creates a new file. Not sure what is happening. Possible bug of JDK1.4.2_b05 in windowx XP ??

I have aslo used a debugger to further test. The following line always create a file of size 0kb.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
File f = new File(path) also creates a new file.
Uh, really? How do you know this? It creates a File object, sure, but can you actually find a file to go with it? E.g. using Windows Explorer or the ls command or whatever you use to browse files on your system?
Trust me - new File() is not creating a new file on your system. If you run the code I showed above and you see f.exists() returns true, then that means the file already existed. It wasn't created by new File(), or new FileInputStream() - it was created by something else. No file is being created by the code you showed. Really.
At times like this, I wish that the File class had been named FileName instead, since that would be more accurate. It's a bit confusing that you can have a File object for a file that does not exist - but you can.
Hans ter Wal
Greenhorn

Joined: Oct 01, 2003
Posts: 12
Hi y'all,
Just my $0.02 , i've had the exact same problem here. Spec's 1.4.2 running under winxp.
Believe when i say, i get new files in my explorer, I 100% sure because i specified the file path wrong, after looking it created a new file.
Hans


SCJP & SCWCD
Sudhansu Pati
Ranch Hand

Joined: Sep 23, 2003
Posts: 77
Thank you Hans and Jim,
I have done lot of testing around this. I have stopped the execution using JBuilder's debugger at the following line
FileInputStream f = new FileInputStream(path);

As soon as I execute the above line (using the Debugger), the file specified in path is created in windows explorer.
Weird issue in windows XP. I have tested with JDK 1.4.1 too and same issue.
I am specifying the Database Location using a GUI and setting it as a property in suncertify.properties file. I would like to capture the error as FileNotFoundException in the Database layer and propagate it to the client tier to handle it.
Now I have no way to handle this error because a 0KB file is created in the specified directory. I don't know which machine sun is going to test my assignment. That means I have to do the testing in Solaris too ? Any my application will take a completely different route in Solaris ? What happened to "Write once and Run Anywhere" tagline ? Is that still valid ?
[ November 13, 2003: Message edited by: Suds Pati ]
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
set the file to the abstract path name and perform a file.exists()
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
one more thing, if you read the documentation for File, you will notice
that every single constructor says "Creates a new file instance....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Bill]: one more thing, if you read the documentation for File, you will notice that every single constructor says "Creates a new file instance....
No, they say "Creates a new File instance...". And they use a <code> font for File. This is an important distinction - they're talking about an instance of the File class. They're not talking about a physical file that you can see with Windows Explorer.
Suds: This still seems really, really unlikely to me. You say you're getting this behavior from both FileInputStream and File? That would be two major, major bugs I think. It seems much more likely that something else is going on here. Try hitting View -> Refresh on Windows Explorer just before the new FileInputStream() line. Also - do you have other threads in your program? Is it possible there's another thread that creates a RandomAccessFile or FileOutputStream, and this thread is executing while you''ve got the new FileInputstream() stopped in the debugger?
Here's some simple test code for you:

[ November 14, 2003: Message edited by: Jim Yingst ]
Sudhansu Pati
Ranch Hand

Joined: Sep 23, 2003
Posts: 77
Hi Jim,
I apologize for the confusion. You are absolutely right and a big THANK YOU. Another thread was reading the following line and creating that 0KB database file.
RandomAccessFile raf = ...
This time I put System.out.println() statement in many places and used the debugger. Then I found the issue.
Hans ter Wal
Greenhorn

Joined: Oct 01, 2003
Posts: 12
Hi y'all,
That did the trick, I also meant I was using a RandomAccessFile.
I thought it worked the same but afterreading the javadoc on it:

"rw" Open for reading and writing. If the file does not already exist then an attempt will be made to create it.

and I had:

Thanks for clearing things up guys!
Hans
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Bill/Jim/Hans/Suds
According your above posts,do I really need read Properties file to get
the file name of database(dbxx.db)?
If this,what format should I store it into suncertify.properties file and assign the string to RandomAccessFile variable as a argument?
Regards,
Richard


Regards, Richard
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234

According your above posts,do I really need read Properties file to get
the file name of database(dbxx.db)?
If this,what format should I store it into suncertify.properties file and assign the string to RandomAccessFile variable as a argument

I thought it was important default information so I stored it in the
properties file. I read it in as a string. After I did some validations
I created a file instance:
(File file = new File(fileNameFromPropertiesFile)) then wrapped
my RandomAccessFile around the file (raf = new RandomAccessFil(file,"rw")).
BTW, I created a file instance first to see if the file even existed to throw the proper exception. But there is probably a better way for this part.
Sudhansu Pati
Ranch Hand

Joined: Sep 23, 2003
Posts: 77
According your above posts,do I really need read Properties file to get
the file name of database(dbxx.db)?
If this,what format should I store it into suncertify.properties file and assign the string to RandomAccessFile variable as a argument

Hi Richard,
In my case it is a requirement (Page 5 of Bodgitt and Scarper). So I have to provide a GUI to the users to enter the Database File Location, and that location has to be stored in a properties file.
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Suds

In my case it is a requirement (Page 5 of Bodgitt and Scarper). So I have to provide a GUI to the users to enter the Database File Location, and that location has to be stored in a properties file.

In my URLyBird assignment(Part of "Deliverables"),there are states:

All configuration must be done via a GUI, and must be persistent between runs of the program. Such configuration information must be stored in a file called suncertify.properties which must be located in the current working directory.

As you said,does those really mean that I have to read file name from "suncertify.properties" and write configuration info with GUI components?
Please clarify that.
Regards,
Richard
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234

quote:
----------------------------------------------------------------------------
All configuration must be done via a GUI, and must be persistent between runs of the program. Such configuration information must be stored in a file called suncertify.properties which must be located in the current working directory.
----------------------------------------------------------------------------
As you said,does those really mean that I have to read file name from "suncertify.properties" and write configuration info with GUI components?
Please clarify that.

Read up on the java.util.Properties package. This is all you need.
To store the file and read the file assume current working directory.
In essence, all you are doing is reading and writting strings. And
this is even taken care of for you by the Properties package.
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Bill
In your above post,
In my case it is a requirement (Page 5 of Bodgitt and Scarper). So I have to provide a GUI to the users to enter the Database File Location, and that location has to be stored in a properties file.

You mean that :
1.I must get the data file location (physical path) through enter file name
or JFileChooser.
2.I must store the string containing the location into properties file.
3.I must store other required information into properties file.
I agree and I will do so.
But from No.3,what info should I store into the properties file?
Please give a example.
Regards,
Richard
Sudhansu Pati
Ranch Hand

Joined: Sep 23, 2003
Posts: 77
Hi Richard,

You mean that :
1.I must get the data file location (physical path) through enter file name
or JFileChooser.
2.I must store the string containing the location into properties file.
3.I must store other required information into properties file.
I agree and I will do so.

Since I have spent way too much time in this assignment, I don't want to take any risk to fall under "automatic failure". Thats why I have provided a clean way for the users to store the properties (part of that is a requirement). My solution is as follows,
* When the clients start the application for the first time, shows them a dialog box to enter the database name and port #. Then that configuration is stored in suncertify.properties in the current working directory. Next time the application automatically loads the properties from suncertify.properties.
* When the application is started in non-networked mode, the application shows a dialog to enter the database location. Once the user enters the database location, the location property is stored into suncertify.properties in the current working directory.
* when the application is started in the server mode, the application shows a dialog box to enter the port # and the database location. Similarly these properties are stored in suncertify.properties in the current working directory
You can use a JFileChooser, but I wanted to use the Dialog to keep things simple.


But from No.3,what info should I store into the properties file?
Please give a example.

I think this is a requirement for the certification, the database location should be sored in the properties file.
Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileInputStream creating a new File ??