【发布时间】:2017-05-30 02:12:14
【问题描述】:
运行此代码后,我总是打印出Exception happened while loading 'Sokoban.txt',这意味着总是捕获IOException。
我正在尝试将 Java 项目转换为 Java FX 应用程序,但不明白为什么 txt 文件的加载在这里不再起作用。
我将不胜感激任何提示或帮助
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);
}
}
编辑:运行输出
ant -f C:\Users\Nouri\Desktop\Sokobangame jfxsa-run 在里面: 删除:C:\Users\Nouri\Desktop\Sokobangame\build\built-jar.properties deps-jar: 更新属性文件:C:\Users\Nouri\Desktop\Sokobangame\build\built-jar.properties 编译: 检测到 JavaFX Ant API 1.3 版 jfx 部署: 罐: 将 12 个文件复制到 C:\Users\Nouri\Desktop\Sokobangame\dist\run822624664 jfx 项目运行: 执行 C:\Users\Nouri\Desktop\Sokobangame\dist\run822624664\Sokobangame.jar 使用平台 C:\Program Files\Java\jdk1.8.0_91\jre/bin/java 加载“Sokoban.txt”时发生异常 级别的格式无效
编辑2:
catch (IOException e) {
System.out.println("Exception happened while loading 'Sokoban.txt'");
e.printStackTrace();
return false;
}
运行输出:
C:\Users\Nouri>cd C:\Users\Nouri\Desktop\Sokobangame\dist
C:\Users\Nouri\Desktop\Sokobangame\dist>java -jar Sokobangame.jar 加载“Sokoban.txt”时发生异常 java.nio.file.NoSuchFileException: sokoban.txt 在 sun.nio.fs.WindowsException.translateToIOException(未知来源) 在 sun.nio.fs.WindowsException.rethrowAsIOException(未知来源) 在 sun.nio.fs.WindowsException.rethrowAsIOException(未知来源) 在 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(未知来源) 在 java.nio.file.Files.newBufferedReader(未知来源) 在 sokobangame.Sokobangame.loadLevel(Sokobangame.java:52) 在 sokobangame.Sokobangame.start(Sokobangame.java:243) 在 com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 在 com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 在 java.security.AccessController.doPrivileged(本机方法) 在 com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 在 com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 在 com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在 com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 在 java.lang.Thread.run(Unknown Source) 级别的格式无效
【问题讨论】:
-
什么是异常消息/堆栈跟踪?
-
如果你想知道为什么会抛出异常,那么忽略它是错过目标的最好方法。打印它的堆栈跟踪。它会告诉你问题出在什么地方。
-
查看编辑:运行输出
-
参见编辑 2:stacktrace
-
如果从 .jar 文件运行,则不能使用
Path;您需要使用Class.getResourceAsStream 读取数据。如果您不是从 .jar 文件运行,则需要熟悉当前目录和相关文件名,这是计算中的基本概念。