I have a bunch (thousands) of resources in an RDF/XML file. I am writing a certain subset of the resources to files -- one file for each, and I'm using the resource's title property as the file name. However, the titles are every day article, website, and blog post titles, so they contain characters unsafe for a URI (the necessary step for constructing a valid file path). I know of the Jersey UriBuilder but I can't quite get it to work for my needs as I detailed in a different question on SO.

Some possibilities I have considered are:

  • Since each resource should also have an associated URL, I could try to use the name of the file on the server. The down side of this is sometimes people don't name their content logically and I think the title of an article better reflects the content that will be in each text file.
  • Construct a white list of valid characters and parse the string myself defining substitutions for unsafe characters. The downside of this is the result could be just as unreadable as the former solution because presumably the content creators went through a similar process when placing the files on their server.
  • Choose a more generic naming scheme, place the title in the text file along with the other attributes, and tell my boss to live with it.

So my question here is, what methods work well for dealing with a scenario where you need to construct file names out of strings with potentially unsafe characters? Is there a solution that better fills out my constraints?


Choose a more generic naming scheme, place the title in the text file along with the other attributes, and tell my boss to live with it.

This will be the best solution, as not only do you have to deal with encoding and decoding the filenames, you have to deal with filenames that might be too long or not meet your filesystem limitations as well, depending on platform.

There is no real benefit in having huge, and now partially encoded filenames on the filesystem. They won't do you any good as a human trying to dig through them with ls. If you are already saving other attributes, the title of the resource is just that another attribute. Your storage solution should be the way you search for things, not the file system.

The titles of the resource and just that titles, metadata, anything else will be polluting your domain. Keep the attributes together, figure out how to full text search them in your storage layer and be done with it, then you can full text search all the attributes. The filesystem isn't a generalized database.


I would suggest you use regular expression for this job, you can create regular expression which matches all valid characters like alphabets, number and some special charater (be sure to use all matches and not the first one only). This way you will remove all uinsafe character but this has its downside if you miss some safe character in you regex. Also you can replace unsafe characters which you know by safe character like '@'(not sure if its unsafe in your case) with '[AT]' this way meaning of name will also not be lost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.