Я пытаюсь загрузить данные приложения с текстом из файла, используя метод loadPlayerDataFromFile
:java.nio.file.NoSuchFileException:
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% бб% d0% b5% d0% BA% d1% 81% d0% b0% d0% шд % d0% b4% d1% 80 \ TowerDefense \ TDv2 \ bin \ application \ resources \ PlayerList.xml at sun.nio.fs.WindowsException.translateToIOException (Неизвестный источник) на sun.nio.fs.WindowsException.rethrowAsIOException (Unknown Source) в sun.nio.fs.WindowsException.rethrowAsIOException (Unknown Source) в sun.nio.fs.WindowsFileSystemProvider.newByteChannel (Неизвестный источник) в java.nio .file.Files.newByteChannel (Неизвестный источник) в java.nio.file.Files.newByteChannel (Неизвестный источник) в java.nio.file.spi.FileSystemProvider.newInputStream (Неизвестный источник) в файле java.nio.file. Files.newInputStream (Неизвестный источник) в java.nio.file.Files.newBufferedReader (Неизвестный источник) в application.util.FileUtil.readFile (FileUtil.java:19) в application.GameApp.loadPlayerDataFromFile (GameApp.java: 242) в application.GameApp.start (GameApp.ja va: 85) на com.sun.javafx.application.LauncherImpl $ 5.run (Неизвестный источник) на com.sun.javafx.application.PlatformImpl $ 5.run (Неизвестный источник) на com.sun.javafx.application. PlatformImpl $ 4 $ 1.run (Неизвестный источник) на com.sun.javafx.application.PlatformImpl $ 4 $ 1.run (Неизвестный источник) на java.security.AccessController.doPrivileged (собственный метод) на com.sun.javafx.application .PlatformImpl $ 4.run (Неизвестный источник) в com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (Неизвестный источник) в com.sun.glass.ui.win.WinApplication._runLoop (собственный метод) в com .sun.glass.ui.win.WinApplication.access $ 100 (Неизвестный источник) в com.sun.glass.ui.win.WinApplication $ 3 $ 1.run (Неизвестный источник) в java.lang.Thread.run (Unknown Source)
Имеет ли файл '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' существуют? :П –