我试图使用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");
}
}当我试图运行应用程序时,它会显示以下错误消息:
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(未知源)
发布于 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检查它是否正确的文件路径。
https://stackoverflow.com/questions/20470226
复制相似问题