【问题标题】:Java-Reading and Writing to Files-File not FoundJava-读写文件-找不到文件
【发布时间】:2018-02-01 17:38:09
【问题描述】:

所以我正在开发这个游戏,我的高分存储在一个文本文件中。我正在关注 newboston 关于如何读取和写入文件的教程。现在我正在测试 read 类是否有效并且它不工作。当我完全按照原样放置文本并且文件在我的包中时,它说找不到文件,所以我不必说路径。这是我的游戏中使用读写类的方法代码:

private int getHighScore(int change, int newScore) {
        if (change==1) {
            readFile r=new readFile();
            r.openFile();
            String fileScore=r.readtext();
            r.closeFile();
            int score=Integer.parseInt(fileScore);
            return score;
        }
        else {
            writeToFile w=new writeToFile();
            w.openFile();
            w.addRecords(newScore);
            w.closeFile();
            return newScore;
        }
    } 

这里是读取类:

import java.io.*;
import java.util.*;
public class readFile {
    private Scanner read;

    public void openFile() {

        try {
            read=new Scanner(new File("highScore.txt"));
        }
        catch(Exception e){
            System.out.println("Could not find file.");
        }
    }
    public String readtext() {
        String score=read.next();
        return score;
    }
    public void closeFile() {
        read.close();
    }
}

这里还有写文件。我担心这个类可能不起作用,因为它看起来可能会创建一个新文件并写入该新文件,而我只是想写入一个我已经称为“highScore.txt”的现有文件无论如何这里是写类:

import java.util.*;
public class writeToFile {
    private Formatter x;
    public void openFile() {
        try {
            x=new Formatter("highScore.txt");
        }
        catch(Exception e) {
            System.out.println("Can not open that file.");
        }
    }
    public void addRecords(int newScore) {
        String score=""+newScore;
        x.format("%s", score);
    }
    public void closeFile() {
        x.close();
    }
}

所以我想知道为什么它不起作用并提前感谢。

【问题讨论】:

  • 你能发布堆栈跟踪吗?
  • 要找出找不到文件的原因,请使用File 类中的.exists()getPathgetAbsolutePath 方法。请参阅:docs.oracle.com/javase/7/docs/api/java/io/File.html 考虑使用boolean 而不是int 作为change 中的change 变量@。
  • 如何发布堆栈跟踪?
  • 哦,堆栈跟踪是这样的:线程“main”中的异常找不到文件。 java.lang.NullPointerException at readFile.readtext(readFile.java:16) at Game.getHighScore(Game.java:474) at Game.updateTitle(Game.java:501) at Game.(Game.java:107 ) 在 Game.main(Game.java:57)
  • 仍然有找不到文件的问题,如果我尝试使用路径仍然存在。我试过does文件存在,它说的是假的意思是它不存在,即使它确实存在。

标签: java file readfile writefile


【解决方案1】:

如果您的文件存在于 ReadFile 和 WriteFile 所在的同一位置。然后你可以使用下面的代码。

URL url =   getClass().getResource("highScore.txt"); 
Scanner read=new Scanner(new File(url.getPath()));

【讨论】:

  • 但是你怎么往那个文件里写东西呢?我在文本文件的第一行只有一个数字。我需要写的只是在旧分数上写下新分数,所以现在只是新分数。
  • 另外,您的 url 示例说,当我将文件名放入其中时,文件无法解析为一种类型。
  • 波士顿的新方法为什么不起作用?正在使用的文档站点的示例是什么?
  • 看起来该文档站点需要路径,我没有路径,因为该文件以及我的所有图片等都在我的项目包中。我不必有路径。
【解决方案2】:

好的,我解决了。问题是位置错误,我仍然不必给出路径。所以基本上我看了这个帖子:The system cannot find the file specified in java

线程的一部分是列出目录中的文件。当我这样做时,我注意到它提到的所有内容都在项目文件夹中。我将我的文件从 src 文件夹移动到项目文件夹并且它工作。因此,如果您遇到问题,您知道文件名拼写正确但 get 文件不存在,请确保您的文件在项目文件夹中,而不是在 src 文件夹中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2013-04-17
    相关资源
    最近更新 更多