I wanted to use SELECT ... WHERE LIKE query against my file’s path (VARCHAR) in my database.
But these files can be inserted to Database from Windows and UNIX/LINUX so file separator can differ, that makes WHERE LIKE operator useless.
I tried to convert paths in my DTO set() method like:
public static final String FILE_SEPARATOR = File.separator;
public static final String ESCAPED_FILE_SEPARATOR = Pattern.quote(FILE_SEPARATOR);
public static String createPortablePath(String path) {
if ((FILE_SEPARATOR).equals("/")) {
return path;
} else {
return path.replaceAll(ESCAPED_FILE_SEPARATOR, "/");
}
This works, but also makes program behaves differently, and it is hard to determine why it is happening (probably there are some lines trying to compare file DTO’s paths as string without creating File object from it ). Is there a solution for this problem – I mean maybe something like File.setReturnedFilePathSeparator(char separator); so all File objects will use one type of separator when getting path from it.
You could store all paths as URIs. This would guarantee you a uniform representation independent of the OS. You wouldn’t have to care about file separators any longer.