> No a co takto na vystup z File pouzit jednoduchy String.replace?
> Nestacilo by ti to?
Ano, ale trosku sofistikovaneji (viz nize)
> public class FileTest {
> public static void main(String[] args) {
> final String fileName = "c:/DesetJezku";
> System.out.println(transformFilePath(fileName));
> }
>
> private static String transformFilePath(String fileName) {
> return new
> File(fileName).getAbsolutePath().replace(File.separatorChar,'/');
> }
> }
V podstate je to korektni, jen to ma jednu nevyhodu - na UNIXu je \
normalni znak, ktery muze byt soucasti nazvu souboru. Tudiz by to
generovalo spatny nazev. Tj replacuju to jen, kdyz jsem na platofme
Windows. Delaji to takhle v Eclipse... Tj nejak takhle:
import java.io.File;
public class PathUtil {
/** Constant value indicating if the current platform is Windows */
private static final boolean WINDOWS = java.io.File.separatorChar ==
'\\';
private PathUtil() {
// static class
}
/**
* Converts "\\" path separator to "/" IF running on windows. On UNIXes
this
* method returns the same string (unchanged).
*/
public static String getPathForwardSlashesFromString(String path) {
if (path == null)
throw new IllegalArgumentException("Path cannot be
null");
if (WINDOWS)
return path.replace(File.separatorChar, '/');
else
return path;
}
/**
* Converts File intsance to the universal (slashes only) string
* representation which is accepted by File and other APIs as standard
by
* calling file.getPath() method and converting the backslashes.
*/
public static String getPathForwardSlashes(File file) {
if (file == null)
throw new IllegalArgumentException("File cannot be
null");
String path = file.getPath();
return getPathForwardSlashesFromString(path);
}
/**
* Converts File intsance to the universal (slashes only) string
* representation which is accepted by File and other APIs as standard
by
* calling file.getAbsolutePath() method and converting the backslashes.
*/
public static String getPathForwardSlashesAbsolute(File file) {
if (file == null)
throw new IllegalArgumentException("File cannot be
null");
String path = file.getAbsolutePath();
return getPathForwardSlashesFromString(path);
}
}
LZ
--
Lukas Zapletal
http://lukas.zapletalovi.com