首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.nio.file.NoSuchFileException:

java.nio.file.NoSuchFileException:
EN

Stack Overflow用户
提问于 2013-12-09 12:05:06
回答 1查看 4.6K关注 0票数 0

我试图使用loadPlayerDataFromFile方法从文件中加载带有文本的应用程序数据:

代码语言:javascript
复制
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");
    }
}

当我试图运行应用程序时,它会显示以下错误消息:

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 at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown源)在java.nio.file.Files.newByteChannel(未知源)在java.nio.file.Files.newByteChannel(未知源)在java.nio.file.spi.FileSystemProvider.newInputStream(Unknown源)在java.nio.file.Files.newInputStream(未知源)在java.nio.file.Files.newBufferedReader(未知源)在application.util.FileUtil.readFile(FileUtil.java:19) at application.GameApp.loadPlayerDataFromFile(GameApp.java:242)在com.sun.javafx.application.LauncherImpl$5.run(Unknown来源的application.GameApp.start(GameApp.java:85) )在com.sun.javafx.application.PlatformImpl$4$1.run(Unknown来源(在com.sun.javafx.application.PlatformImpl$4$1.run(Unknown来源)在com.sun.javafx.application.PlatformImpl$4$1.run(Unknown来源)(在java.security.AccessController.doPrivileged(Native方法)(在com.sun.javafx.application)。PlatformImpl$4.运行(未知源)在com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown源)在com.sun.glass.ui.win.WinApplication._runLoop(Native方法)在com.sun.glass.ui.win.WinApplication.access$100(Unknown源)在com.sun.glass.ui.win.WinApplication$3$1.run(Unknown源)在java.lang.Thread.run(未知源)

EN

回答 1

Stack Overflow用户

发布于 2013-12-09 12:15:04

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20470226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档