首页 文章

java.nio.file.NoSuchFileException:

提问于
浏览
0

我正在尝试使用 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上的sun.nio.fs.WindowsException.rethrowAsIOException(未知来源)sun.nio.fs.WindowsException.translateToIOException(未知来源)上的\ TowerDefense \ TDv2 \ bin \ application \ resources \ PlayerList.xml位于java.nio.file.Files.newByteChannel(未知来源)的sun.nio.fs.WindowsFileSystemProvider.newByteChannel(未知来源)的.rethrowAsIOException(未知来源)位于java的java.nio.file.Files.newByteChannel(未知来源)位于application.util.FileUtil.readFile的java.nio.file.Files.newBufferedReader(未知来源)的java.nio.file.Files.newInputStream(未知来源)中的.nio.file.spi.FileSystemProvider.newInputStream(未知来源) (FileUtil.java:19)at application.GameApp.loadPlayerDataFromFile(GameApp.java:242)at application.GameApp.start(GameApp.java:85)at com.sun.javafx.application.LauncherImpl $ 5.run(Unknown Source)在com.sun.javafx.application.PlatformImpl $ 5.run(未知来源)at com.sun.javafx.application.PlatformImpl $ 4 $ 1.run(未知来源)at com.sun.javafx.application.PlatformImpl $ 4 $ 1.run(未知来源)at java.security.AccessController . do.srivileged(本地方法)at com.sun.javafx.application.PlatformImpl $ 4.run(未知来源)com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(未知来源)com.sun.glass.ui.win .inApplication._runLoop(Native Method)位于com.sun.glass.ui.win.WinApplication.access $ 100(未知来源)位于com.sun.glass.ui.win.WinApplication $ 3 $ 1.run(未知来源),位于java . lang.Thread.run(未知来源)

1 回答

  • 0

    首先检查你的文件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检查它的文件路径是否正确 .

相关问题