Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URL->filename conversion problem

 
Duncan Fawkes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried using some of the other methods of the File class, like getAbsolutePath(), getCanonicalPath(), or getParent() ?
 
Duncan Fawkes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic