wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes URL->filename conversion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "URL->filename conversion problem" Watch "URL->filename conversion problem" New topic
Author

URL->filename conversion problem

Duncan Fawkes
Greenhorn

Joined: Jul 17, 2001
Posts: 10
Hi all. I have the following piece of code:

String sFilename = "";
URL u = ClassLoader.getSystemResource("duncan.txt");
if (u != null)
{
sFilename = new File(u.getFile()).getPath();
}

The intention is that it finds this file, and the gets the filename, which is subsequently passed into a native function.
In Windows this works fine, for example after the code is run, sFilename has the value 'C:\test\duncan.txt' which is great and exactly what I want. However, on Unix sFilename has the value '/FILE0/+/qaworld.ini' which is of no use to me (I was expecting something like /home/test/duncan.txt). What do I have to do to get the filename in the correct format?? Any help would be appreciated. Cheers,
dUNC
deadsea
Greenhorn

Joined: Nov 20, 2001
Posts: 2
There are many places where that will not work. All system resources do not have a file name. For example if your class and text file were inside a jar file.
It is better to work with system resources by getting a stream to that file. If you really need file operations, I would suggest writing it to a temporary file. Most of the time a stream suffices.
Duncan Fawkes
Greenhorn

Joined: Jul 17, 2001
Posts: 10
The issue is that the my native code has a configuration file (the file I am trying to locate) which I need to pass in upon initialisation. So I know that this file exists, and the underlying code is expecting a filename to it - so I need this code to locate it and pass it to the native code (reading it in and then writing it to another file is not really the solution I'm looking for). Is this just not possible??
Thanks in advance,
dUNC
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10
deadsea,
Please read http://www.javaranch.com/name.jsp and re-register. We would like you continue to be able to post here at JavaRanch.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10
Have you tried using some of the other methods of the File class, like getAbsolutePath(), getCanonicalPath(), or getParent() ?
Duncan Fawkes
Greenhorn

Joined: Jul 17, 2001
Posts: 10
Ye, I tried all the different methods (quite a few of them the copiler said didn't exist in the class java.net.URL, which is slightly odd), but nothing seems to work the way I would think. So much for portability I guess. My current idea is to have a properties file that gets loaded using getSystemResourceAsStream, with a property that points to the file I actually want. Hardly ideal, but if my orignal plan just won't work how I think it should, then it will have to do. Cheers,
dUNC
Duncan Fawkes
Greenhorn

Joined: Jul 17, 2001
Posts: 10
Ooops, got in a slight tangle there, hehe.
I tried a variety of different methods of both the URL and File class and was told some of them didn't exist.
Just had another thought, if I use getSystemResourceAsStream, is there anyway to get the filename from the InputStream (which could be a FileInputStream) or from its FileDescriptor? Cheers,
dUNC
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URL->filename conversion problem