27

URL または文字列からファイル名を削除するにはどうすればよいですか?

String os = System.getProperty("os.name").toLowerCase();
        String nativeDir = Game.class.getProtectionDomain().getCodeSource().getLocation().getFile().toString();

        //Remove the <name>.jar from the string
        if(nativeDir.endsWith(".jar"))
            nativeDir = nativeDir.substring(0, nativeDir.lastIndexOf("/"));

        //Load the right native files
        for(File f : (new File(nativeDir + File.separator + "lib" + File.separator + "native")).listFiles()){
            if(f.isDirectory() && os.contains(f.getName().toLowerCase())){
                System.setProperty("org.lwjgl.librarypath", f.getAbsolutePath()); break;
            }
        }

それが私が今持っているもので、うまくいきます。私が知っていることから、「/」を使用しているため、Windowsでのみ機能します。プラットフォームに依存しないようにしたい

4

9 に答える 9

15

別の行で File.separator を使用しています。lastIndexOf() にも使用してみませんか?

nativeDir = nativeDir.substring(0, nativeDir.lastIndexOf(File.separator));
于 2014-02-17T12:45:10.427 に答える
3
File file = new File(path);
String pathWithoutFileName = file.getParent();

パスは「C:\Users\userName\Desktop\file.txt」の場合があります

于 2020-08-09T15:20:39.707 に答える