aspose file tools*
The moose likes Java in General and the fly likes Creating File object problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating File object problem" Watch "Creating File object problem" New topic
Author

Creating File object problem

tim jones
Greenhorn

Joined: Feb 09, 2007
Posts: 26
Hello everybody,

I have tried to create a java File object for an existing file in Windows system. The JVM is running on a Sun Solaris application.
I am unable to get a File object using the below code:

where filenameUpd1 is a path like 'D:\tmp\test'.

I assumed the File object was not creating because of the backslash present and used the below code
to remove them:


I now get a string like ''D:\\tmp\\test' but still the file1.isFile() keeps returning false.

I tried and it returns a string '/'.

Anyway I could resolve the issue, so as to get a 'true' value from file1.isFile()? Or maybe I have done
something wrong. Kindly provide your help.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I'm confused - is this on a Windows or a Solaris system? A "D:" file designation indicates Windows, a "/" file separator indicates Solaris.

More importantly, "new File(...)" creates a File object, not a file. Unless the file existed before you will always get false from isFile() and exists(). If you want to create a new empty file, call createNewFile().
tim jones
Greenhorn

Joined: Feb 09, 2007
Posts: 26
Hello,

This code is present in a jsp file. The Weblogic is running on Sun Solaris system.
I open the web application from a Windows system and thus the Windows File path.

I need to confirm whether a File exists at a given FilePath in windows.
So I use the Windows File Path, try creating a File object, if file exists at the FilePath then file1.isFile() should return true.
Hope this provides more clarity.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You fundamentally misunderstand how JSP works: JSP code running on the server does not have access to the client-side file system.
tim jones
Greenhorn

Joined: Feb 09, 2007
Posts: 26
How would I upload a file from client side to server then?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
By using an input element with type=file in an HTML form.
tim jones
Greenhorn

Joined: Feb 09, 2007
Posts: 26
This is my requirement:
I need to allow browsing a file only if no file exists at a given location.
Any ideas on how I can go about it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18125
    
    8

tim jones wrote:This is my requirement:
I need to allow browsing a file only if no file exists at a given location.
Any ideas on how I can go about it?


Surely that's a new question? Your original question was about JSPs and file uploading and so on. This can't possibly have anything to do with that. Or can it? Perhaps you can explain in more detail.
tim jones
Greenhorn

Joined: Feb 09, 2007
Posts: 26
Hello Paul,

Well this is the scenario, the user needs to upload a file. In an earlier page, the file path is already collected.
Now before uploading to server, if the given filepath is valid, upload should happen or else browse option needs to be enabled for the user
to upload another file.

I did not know about JSP restrictions to access file systems. Seems quite logical now. Thanks to Tim to point that one out.
That was why I was trying to check if file is valid by creating a File object out of the filepath and validating it (either using exists() or isFile()).

Please help with your suggestions.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18125
    
    8

First suggestion: get rid of that requirement. It's nonsense anyway. The way to allow a user to upload a file has already been posted in this thread. It isn't the responsibility of the server to manage the process whereby the user chooses what file to upload, nor is it even possible for the server to do that.

It's also impossible for the user to upload a file which doesn't exist. The browser takes care of that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating File object problem
 
Similar Threads
renameTo() and delete() not working
Problem trying to create a Postscript File
File Exists
Windows File Paths
file renameTo() method