簡體   English   中英

java.nio.file.NoSuchFileException:

[英]java.nio.file.NoSuchFileException:

我正在嘗試使用loadPlayerDataFromFile方法從文件加載文本中的應用程序數據:

public void loadPlayerDataFromFile(File file) {
    XStream xstream = new XStream();
    xstream.alias("player", Player.class);

    try {
        String xml = FileUtil.readFile(file);

        ArrayList<Player> playerList = (ArrayList<Player>) xstream
                .fromXML(xml);

        playerData.clear();
        playerData.addAll(playerList);

        setPlayerFilePath(file);
    } catch (Exception e) { // catches ANY exception                        
         Dialogs.showErrorDialog(primaryStage,
         "Could not load data from file:\n" + file.getPath(),
         "Could not load data", "Error", e);             
    }
}
public File getPlayerFilePath() {
    Preferences prefs = Preferences.userNodeForPackage(GameApp.class);
    prefs.put( "playerPath", getClass().getResource("resources/PlayerList.xml").getFile());
    String filePath = prefs.get("playerPath", "default");
    if (filePath != null) {
        return new File(filePath);
    } else {
        return null;
    }
}

public void setPlayerFilePath(File file) {
    Preferences prefs = Preferences.userNodeForPackage(GameApp.class);
    if (file != null) {
        prefs.put("filePath", file.getPath());
        // Update the stage title
        //primaryStage.setTitle("Player - " + file.getName());
    } else {
        prefs.remove("filePath");
        // Update the stage title
        //primaryStage.setTitle("Player");
    }
}

當我嘗試運行該應用程序時,它顯示以下錯誤消息:

java.nio.file.NoSuchFileException:C:\\ Users \\%d0%90%d0%bb%d0%b5%d0%ba%d1%81%d0%b0%d0%bd%d0%b4%d1%80 \\位於sun.nio.fs.WindowsException.translateToIOException(未知來源)處的TowerDefense \\ TDv2 \\ bin \\ application \\ resources \\ PlayerList.xml(位於sun.nio.fs.WindowsException處的sun.nio.fs.WindowsException.rethrowAsIOException(未知源)。 java.nio.file.Files.newByteChannel(未知源)處的java.nio.file.Files.newByteChannel(未知源)處的sun.nio.fs.WindowsFileSystemProvider.newByteChannel(未知源)處的rethrowAsIOException(未知源)。 nio.file.spi.FileSystemProvider.newInputStream(未知源),位於java.nio.file.Files.newInputStream(未知源),位於java.nio.file.Files.newBufferedReader(未知源),位於application.util.FileUtil.readFile( FileUtil.java:19)位於應用程序.GameApp.loadPlayerDataFromFile(GameApp.java:242)位於應用程序.GameApp.start(GameApp.java:85)位於com.sun.javafx.application.LauncherImpl $ 5.run(未知源) com.sun.javafx.application.PlatformImpl $ 5 com.sun.javafx.application.PlatformImpl $ 4 $ 1.run(未知源)com.sun.javafx.application.PlatformImpl $ 4 $ 1.run(未知源)處的java.security.AccessController.doPrivileged (本機方法)位於com.sun.glass.ui.com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(未知來源)位於com.sun.glass.ui.win的com.sun.javafx.application.PlatformImpl $ 4.run(未知來源)。 com.sun.glass.ui.win.WinApplication.access $ 100(未知源)的com.sun.glass.ui.win.WinApplication $ 3 $ 1.run(java.lang處的WinApplication._runLoop(本機方法) .Thread.run(未知來源)

首先,檢查文件C:\\ Users \\%d0%90%d0%bb%d0%b5%d0%ba%d1%81%d0%b0%d0%bd%d0%b4%d1%80 \\ TowerDefense \\ TDv2 \\ bin \\ application \\ resources \\ PlayerList.xml是否存在。 如果存在,則從jar中檢查文件路徑是否正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM