我正在尝试使用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:\用户\%D0%90%D0%BB%D0%B5%D0%BA%D1%81%D0%B0%D0%BD%D0%B4%D1%80 \塔防游戏\ TDv2 \ BIN \应用\资源\ 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) 在sun.nio.fs.WindowsFileSystemProvider.newByteChannel(未知 来自)java.nio.file.Files.newByteChannel(未知来源)at java.nio.file.Files.newByteChannel(未知来源)at java.nio.file.spi.FileSystemProvider.newInputStream(未知来源) 在java.nio.file.Files.newInputStream(未知来源)at java.nio.file.Files.newBufferedReader(未知来源)at application.util.FileUtil.readFile(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(未知来源)at 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.doPrivileged(Native Method)at com.sun.javafx.application.PlatformImpl $ 4.run(未知来源)at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(未知来源)at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at com.sun.glass.ui.win.WinApplication.access $ 100(未知来源)at com.sun.glass.ui.win.WinApplication $ 3 $ 1.run(未知来源)at java.lang.Thread.run(未知来源)
答案 0 :(得分: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中检查它的文件路径是否合适。