保存txt文件时找不到文件错误 [英] file not found error when saving txt file

查看:64
本文介绍了保存txt文件时找不到文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with a file saving alogrithm.

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)

idk出了什么问题,这是一种用于小型俄罗斯方块游戏的保存算法.

idk what is wrong, this is a saveing algorithm for a small tetris game.

文件保存在以下位置:


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

我已经在该课程中添加了主课程. 将其粘贴到您的ide中并运行它,我会遇到很多错误

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());


    }
}

推荐答案

问题是您没有将有效路径传递给new File. this.getClass().getResource("LineHighscores.txt")返回URL对象.然后,您将获得URL对象的String表示形式.

The issue is that you are not passing a valid path to new File. this.getClass().getResource("LineHighscores.txt") returns a URL object. You're then getting a String representation of the URL object.

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

But when calling new File(String), the constructor expects a path name, not a String containing a file:// URL. Since "file:\C:\Users\Koen\Dropbox\2TI\vgo\Eindwerk\Tetris\build\classes\tetris\LineHighscores.txt" is not a valid Windows path name, an exception is thrown.

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

There are a few ways to address this. An easy method would be to use getResourceAsStream instead of getResource().toString(). Then change

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可以为您提供内容.

This will allow you to read directly from the input files. And loadHighScores no longer cares whether the data is coming from an actual File, or another location. This can be handy if you ever need to read from a resource within a jar file on the classpath. You won't be able to read it directly as a file, but using getResourceAsStream can give you the content.

这篇关于保存txt文件时找不到文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆