On Tue, Oct 21, 2008 at 05:31:48PM +0200, Lukáš Zapletal wrote:
>
> 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
To je preci jedno. Stejne se new File() musi predhodit KOREKTNI nazev souboru
vcetne cesty a File musi spravne rozeznat, co je a co neni platne pro jmeno
souboru. A to, co se replacuje, je file.separator, nikoli Windows file
separator. Takze na UNIXu se / preplacne /, takze se nic nestane.
Na Woknech se \ preplacne za / a vysledek je ten, ktery je ocekavan.
Problem samozrejme nastane, pokud by nahodou jmeno souboru na nejakem obskurnim
systemu mohlo obsahovat '/'. Ale to by to stejne nefungovalo uz z principu.
tapik
> 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