0

I have two strings. One names a folder's absolute (or relative) path. The other names a file name with potentially unsafe characters:

String folder = "C:\a\b\c\dir";

Sting file = "file name: contains-unsafe_characters?" + ".txt"

I want to smash these to strings together literally in the form:

String absPath = folder + SYSTEM_INDEPENDENT_PATH_SEPERATOR + convertToPercentOctal(folder);

Now, there are probably one million approaches to try to do this:

UriBuilder folderBuilder = UriBuilder.fromPath(folder);
UriBuilder fileBuilder   = UriBuilder.fromPath(file);
URI completeFileName = folderBuilder.build().resolve(fileBuilder.build());

or:

URI completeFileName = UriBuilder.fromPath(folder).path(file).build();

etc.

The first block of code fails because there may be a : in the file name. If that is the case, UriBuilder will try to parse it as the optional scheme:. The second way fails because it returns the first and second half converted to %octal notation separated by a system dependent '/' character.

Is there a way concatenate a two strings, convert the result to a writable file name, and return a valid URI I can use to create the file?

dcow
  • 7,765
  • 3
  • 45
  • 65
  • This might be useful for you: http://stackoverflow.com/questions/6730009/validate-a-file-name-on-windows – Eng.Fouad Jul 09 '12 at 23:06
  • @Eng.Fouad Sort of, thanks.. but it's for Windows. I'm looking for a system independent method as Java is supposed mask system dependence (at least with writing to the file system).. – dcow Jul 10 '12 at 00:36

0 Answers0