的Java拋出一個異常:的Java IO無法讀取輸入文件在Java小程序讀取圖片文件時
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
at UI.readMatrix(UI.java:27)
at MazeViewControl.init(MazeViewControl.java:45)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:680)
的形象,而作爲Java應用程序運行的IO工作正常:
public class MazeViewControl extends JApplet {
UI ui;
MazeView view;
Maze maze;
int theme;
int option;
String filename="src/maze0.bmp";
public void init() {
ui=new UI();
maze=new Maze();
try {
ui.readMatrix("src/maze0.bmp", maze, 1, 0, 0,0,319,239);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
public class UI {
public UI(){
return;
}
/**
* read and construct the map from a txt file
* @param filename
* @throws IOException
*/
public void readMatrix(String filename, Maze m, int theme, int option, int sx, int sy, int ex, int ey) throws IOException{
/* pre-read the file*/
//Create file for the source
File input = new File(filename);
int rows=0;
int columns=0;
//Read the file to a BufferedImage
// Surround this with try/catch or have your method
// throw an exception
System.out.println(filename);
BufferedImage image = ImageIO.read(input);
謝謝!文件選擇器能夠通過這種方式建立連接嗎? –
https://www.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem –