tags:

views:

34

answers:

2

I am working with files (reading, writing and copying) in my Java application, java.io.File and commons-io were perfect for this kind of tasks.

Right now, I can link to HTML in this way:

Z:\an absolute\path\to\a\file.html

But, I need to provide support for anchors too:

Z:\an absolute\path\to\a\file.html#anchor

keeping the system-independence obtained by using java.io.File. So, I will need to extract the path and the anchor, I wonder whether it will be as easy as searching for a sharp occurrence.

+1  A: 

java.io.File includes a constructor that accepts a URI, which can represent all kinds of resources, included URLs and local files (see the rfc). URI's also meets your requirements of supporting anchors, and extracting path information (through instance.getPath()).

Dana the Sane
When trying to convert from java.io.File to java.net.URI using java.io.File.toURI() method the sharp (#) character is escaped. The final users don't care about URIs, they care about using their always-known-windows paths. In my case this won't be the solution, unless there is a more straighforward way to manage this issue.
Alexander
I may have been unclear. I'm suggesting that you refactor your code to use URI's for all resources and to convert them as necessary, depending on what you need to do. This way the users can deal with local paths, or URL's, you just need to create the appropriate object for performing io. If your code doesn't make it simple to do that, then I see your dilemma.
Dana the Sane
A: 
File f = new File("Z:\\path\\to\\a\\file.html#anchor");
String anchor = f.toURL().getRef(); //note: toURL is deprecated

If you look at the java source you will see that it is as simple as:

String file = "Z:\\path\\to\\a\\file.html#anchor";
int ind = file.indexOf('#');
String anchor = ind < 0 ? null: file.substring(ind + 1);
dogbane
This implies refactoring the whole code and taking care of not been able to use common java.io.File methods.
Alexander