gpt4 book ai didi

java - 保存txt文件时出现文件未找到错误

转载 作者:行者123 更新时间:2023-12-01 14:59:50 27 4
gpt4 key购买 nike

我的文件保存算法有问题。

error:
dec 11, 2012 4:56:18 PM tetris.FileIO loadHighscores
SEVERE: null
java.io.FileNotFoundException: file:\C:\Users\Koen\Dropbox\2TI\vgo\Eindwerk\Tetris\build\classes\tetris\LineHighscores.txt (De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:656)
at tetris.FileIO.loadHighscores(FileIO.java:44)
at tetris.FileIO.getLineScores(FileIO.java:31)
at tetris.FileIO.main(FileIO.java:69)

Exception in thread "main" java.lang.NullPointerException
at tetris.FileIO.loadHighscores(FileIO.java:49)
at tetris.FileIO.getLineScores(FileIO.java:31)
at tetris.FileIO.main(FileIO.java:69)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

不知道哪里出了问题,这是一个小型俄罗斯方 block 游戏的保存算法。

文件保存在:

...\Eindwerk\Tetris\build\classes\tetris\LineHighscore.txt...\Eindwerk\Tetris\build\classes\tetris\TimeHighscore.txt

I've added a main class to this class.paste it into your ide and run it, i get many errors

public class FileIO   {
private File file;
private Scanner filescScanner, lineScanner;
private Writer fileWriter, lineWriter;
private String[][] data;


public FileIO () {
String[][] data = new String[100][1];
}
public String[][] getLineScores(){
return this.loadHighscores(this.getClass().getResource("LineHighscores.txt").toString());
}
public String[][] getTimeScores(){
return this.loadHighscores(this.getClass().getResource("TimeHighscores.txt").toString());
}

public String[][] loadHighscores(String path){
int x=0;
String test = "";
file = new File(path);

try {
filescScanner = new Scanner(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(file.toString());
while((test=filescScanner.nextLine())!=null) {

lineScanner = new Scanner(test);
lineScanner.useDelimiter("-/-");
System.out.println(lineScanner);
System.out.println(lineScanner.toString());
data[x][0]=lineScanner.next();//name
data[x][1]=lineScanner.next();//data
x++;

}
lineScanner.close();
filescScanner.close();
return data;

}


public static void main(String[] args){
FileIO file = new FileIO();
System.out.println(file.getLineScores());


}
}

最佳答案

问题是您没有将有效路径传递给新文件this.getClass().getResource("LineHighscores.txt") 返回一个 URL 对象。然后,您将获得 URL 对象的字符串表示形式。

但是当调用new File(String)时,构造函数需要一个路径名,而不是包含 file://URL 的字符串。由于“file:\C:\Users\Koen\Dropbox\2TI\vgo\Eindwerk\Tetris\build\classes\tetris\LineHighscores.txt”不是有效的 Windows 路径名,因此会引发异常。

有几种方法可以解决这个问题。一个简单的方法是使用 getResourceAsStream 而不是 getResource().toString()。然后更改

public String[][] loadHighscores(String path){
int x=0;
String test = "";
file = new File(path);

try {
filescScanner = new Scanner(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
}

public String[][] loadHighscores(InputStream resourceStream){
int x=0;
String test = "";

filescScanner = new Scanner(resourceStream);

这将允许您直接从输入文件中读取。并且 loadHighScores 不再关心数据是来自实际文件还是其他位置。如果您需要从类路径上的 jar 文件中读取资源,这会很方便。您将无法直接将其作为文件读取,但使用 getResourceAsStream 可以为您提供内容。

关于java - 保存txt文件时出现文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13829484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com