このコードを実行すると、常にException happened while loading 'Sokoban.txt'
が出力され、IOException
が常にキャッチされます。 私はJavaプロジェクトをJava FXアプリケーションに変換しようとしていますが、なぜtxtファイルの読み込みがもうここではうまくいかないのか分かりません。 私は任意のヒント感謝されるか、役立つだろうJava FXアプリケーションでtxtファイルを読み込んでいる間にcatchのIOExceptionが発生する
package sokobangame;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class Sokobangame extends Application {
private final static int X = 0;
private final static int Y = 1;
private final static char WALL = '#';
private final static char PLAYER = '@';
private final static char BOX = '$';
private final static char GOAL = '.';
private final static char PLAYER_ON_GOAL = '+';
private final static char BOX_ON_GOAL = '*';
private final static char FREE = ' ';
private final static int[] UP = {0, -1};
private final static int[] DOWN = {0, 1};
private final static int[] LEFT = {-1, 0};
private final static int[] RIGHT = {1, 0};
private static char[][] room;
private static int freeBox;
private static int emptyGoal;
private static int[] size = {-1, 0};
private static int[] player;
public static String file = "sokoban.txt";
/**
* Loads the level from the "file" and validate it
*
* @param file path to the file
* @return false iff an error occurs or the level is invalid, true otherwise
*/
private static boolean loadLevel(String file) {
BufferedReader bufferedReader;
try {
bufferedReader = Files.newBufferedReader(Paths.get(file));
bufferedReader.mark(100 * 100);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("line = bufferedReader.readLine()) != null");
size[Y]++;
if (size[X] > -1 && size[X] != line.length()) {
return false;
} else {
size[X] = line.length();
}
}
bufferedReader.reset();
room = new char[size[Y]][];
int i = 0;
while ((line = bufferedReader.readLine()) != null) {
room[i] = new char[line.length()];
for (int j = 0; j < line.length(); j++) {
room[i][j] = line.charAt(j);
}
i++;
// oder room[i++] = line.toCharArray();
}
bufferedReader.close();
}
catch (IOException e) {
System.out.println("Exception happened while loading 'Sokoban.txt'");
return false;
}
for (int i = 0; i < room.length; i++) {
for (int j = 0; j < room[i].length; j++) {
switch (room[i][j]) {
case FREE:
case BOX_ON_GOAL:
case WALL:
break;
case PLAYER_ON_GOAL:
emptyGoal++;
case PLAYER:
if (player != null) {
return false;
} else {
player = new int[]{j, i};
}
break;
case BOX:
freeBox++;
break;
case GOAL:
emptyGoal++;
break;
default:
return false;
}
}
}
return !(player == null || emptyGoal != freeBox);
}
/**
* Prints the level to the output stream
*/
private static void printLevel() {
for (char[] row : room) {
System.out.println(row);
}
}
/**
* Function for vector addition
*
* @param first first vector
* @param second second vector
* @return new vector = first + second
*/
private static int[] add(int[] first, int[] second) {
return new int[]{first[X] + second[X], first[Y] + second[Y]};
}
/**
* Game logic for Sokoban
*
* @return true iff the level was solved, otherwise false
*/
private static boolean game() {
// create new Scanner that reads from console
Scanner input = new Scanner(System.in);
// flag if we quit the program
boolean run = true;
int[] direction;
do {
printLevel();
System.out.println("Do you want to go up, down, left, right or exit the program?");
// check which command was chosen and execute it
switch (input.next()) {
case "w":
case "up":
direction = UP;
break;
case "s":
case "down":
direction = DOWN;
break;
case "a":
case "left":
direction = LEFT;
break;
case "d":
case "right":
direction = RIGHT;
break;
case "exit":
run = false;
continue;
default: // if the user input is not one of our commands print help
System.out.println("Command unknown! Please type up, down, left or right to move or exit to quit this program");
continue;
}
if (!move(direction)) {
System.out.println("You can not go there!");
}
} while (run && emptyGoal != 0 && freeBox != 0);
return run;
}
/**
* Makes a move
*
* @param direction as a vector
* @return true iff it was successful, otherwise false
*/
private static boolean move(int[] direction) {
int[] next = add(player, direction);
switch (room[next[Y]][next[X]]) {
case BOX_ON_GOAL:
case BOX:
int[] behind = add(next, direction);
if (!(room[behind[Y]][behind[X]] == FREE || room[behind[Y]][behind[X]] == GOAL)) {
return false;
}
if (room[next[Y]][next[X]] == BOX_ON_GOAL) {
emptyGoal++;
freeBox++;
}
if (room[behind[Y]][behind[X]] == GOAL) {
room[behind[Y]][behind[X]] = BOX_ON_GOAL;
emptyGoal--;
freeBox--;
} else {
room[behind[Y]][behind[X]] = BOX;
}
if (room[next[Y]][next[X]] == BOX_ON_GOAL) {
room[next[Y]][next[X]] = GOAL;
} else {
room[next[Y]][next[X]] = FREE;
}
case GOAL:
case FREE:
if (room[player[Y]][player[X]] == PLAYER_ON_GOAL) {
room[player[Y]][player[X]] = GOAL;
} else {
room[player[Y]][player[X]] = FREE;
}
player = next;
if (room[player[Y]][player[X]] == FREE) {
room[player[Y]][player[X]] = PLAYER;
} else {
room[player[Y]][player[X]] = PLAYER_ON_GOAL;
}
return true;
default:
return false;
}
}
@Override
public void start(Stage primaryStage) {
if (!loadLevel(file)) {
System.err.println("Level has an invalid format");
return;
}
if (game()) {
System.out.println("Yeah you have solved the level :)");
} else {
System.out.println("You have not solved the level :(");
}
printLevel();
System.out.println("Goodbye");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length > 0) {
file = args[0];
}
launch(args);
}
}
EDIT:実行出力
アリ-f C:\ Users \ユーザーヌーリ\デスクトップ\ Sokobangame jfxsa・ラン のinit: の削除: C:\ユーザーは、ヌーリ\デスクトップ\ Sokobangame \は\ built-jar.properties DEPS-jarファイルを構築:\ 更新プロパティファイル:C: :\ユーザーは、ヌーリ\デスクトップ\ Sokobangame \は\ built-jar.properties コンパイルを構築\します検出されたJavaFX Ant APIイオン1.3 JFX-展開: 瓶: Cに12個のファイルをコピーする:ユーザーはヌーリ\デスクトップ\ Sokobangameの\ distのを\ \: 実行C:\ユーザーは、ヌーリ\デスクトップ\ Sokobangame \ distの\ run822624664 JFX-プロジェクト・ランを\ \ run822624664 \ Sokobangame.jar 使用してプラットフォームのC:\プログラムファイル\のJava \ jdk1.8.0_91ロード 'Sokoban.txt' レベルが無効な形式を持っていながら 例外が起こったのjre/binに/ javaの\
EDIT 2:
catch (IOException e) {
System.out.println("Exception happened while loading 'Sokoban.txt'");
e.printStackTrace();
return false;
}
実行出力:
C:\ Users \ユーザーヌーリ> CDのC:\ Users \ユーザーヌーリ\デスクトップ\ Sokobangameの\ distの
C:\ Users \ユーザーヌーリ\デスクトップ\ Sokobangameの\ distの>のjava -jar sun.nio.fs.WindowsException.translateToIOExceptionで (不明なソース)sokoban.txt をsun.nio.fs.WindowsExceptionで 'sokoban.txt' java.nio.file.NoSuchFileExceptionをロード中にSokobangame.jar 例外が起こりました。 rethrowAsIOException(不明なソース) at sun.nio.fs.WindowsException.rethrowAsIOException(不明なソース) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unkn独自のソース) at java.nio.file.Files.newByteChannel(不明なソース) (java.nio.file.Files.newByteChannel) (java.nio.file.spi.FileSystemProvider.newInputStream) at java.nio.file.Files.newInputStream(不明なソース) at java.nio.file.Files.newBufferedReader(不明なソース) at java.nio.file.Files.newBufferedReader(不明なソース) at sokobangame.Sokobangame .loadLevel(Sokobangame.java:52) at sokobangame.Sokobangame.start(Sokobangame.java:243) at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.java:863) at com。 sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda $ null $ 173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(ネイティブメソッド) at com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 174(PlatformImpl.java:294) com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication。_runLoop(ネイティブメソッド) at com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication.java:191) java.lang.Thread.run(Unknown Source)レベルのフォーマットが無効
例外メッセージ/スタックトレースとは何ですか? – Marvin
例外がスローされる理由を知りたい場合は、それを無視することがあなたの目標を逃す最善の方法です。スタックトレースを出力します。何が問題なのかを教えてくれるでしょう。 –
EDIT:Run Outputを参照 –