0

ファイルから読み書きするプログラムがあります。でファイルを作成しようとしましたmainが、それでも機能しません。

public static void main(String[] args) throws NumberFormatException, 
                                              IOException, 
                                              FileNotFoundException {
    System.out.println("Work in progress");

    File f = new File("Data.txt");
    System.out.println(f.getAbsolutePath());
            // Yes, it's there.


    UI ui = new UI();
    GenericDictionary<Integer> gd = new GenericDictionary<Integer>();
    Repository repo = new Repository("Data.txt", gd);
            // Should work, right ?

今私のリポジトリ:

public Repository (String fileName, GenericDictionary gd) 
                                               throws IOException, 
                                                      NumberFormatException, 
                                                      FileNotFoundException {
    this.fileName = fileName;
    this.gd = gd;


    FileReader input = null;
    BufferedReader  inputBuffer = null;

    try {
        input = new FileReader(this.fileName);
        inputBuffer = new BufferedReader (input);

        String line;
        while ((line = inputBuffer.readLine()) != null) {
            String[] inputData = line.split(",");

            Node<Integer> newNode = new Node<Integer> (
                                       Integer.parseInt(inputData[0]),
                                       Integer.parseInt(inputData[1]));
            this.gd.add(newNode);
        }



    }catch (NumberFormatException nfe){
        System.out.println(
               "Repository could not load data due to NumberFormatException: "
               + nfe); 
    }catch (FileNotFoundException fnfe) {
        System.out.println("File not found, error: " + fnfe);
    }finally {
        inputBuffer.close();
        input.close();
    }
}

これで、ファイルを作成しても、使用したくありません。最初はリポジトリのコンストラクターにありましたが、メインファイルに移動しましたが、それでも成功しませんでした。

これは、Eclipseがコンソール内で出力するものです。

  • 進行中の作業D:\ Info \ Javaworkspace \ Laborator_4 \ Data.txtファイルが見つかりません、エラー:java.io.FileNotFoundException:Data.txt(システムは指定されたファイルを見つけることができません)スレッド"main"java.langで例外が発生しました。 main.app.main(app.java:23)のrepository.Repository。(Repository.java:49)でのNullPointerException
4

4 に答える 4

4

これはあなたが思っていることをしません:

File f = new File("Data.txt");
System.out.println(f.getAbsolutePath());
        // Yes, it's there.

それはディスク上にファイルを作成しません。Fileパス名を表すオブジェクトを作成するだけです。使用する場合:

System.out.println(f.exists());

それが本当に存在するかどうかを示します。

したがってD:\Info\Java workspace\Laborator_4\Data.txt、実際に存在しない限り、例外を取得することは完全に合理的です。ファイルを作成して再試行してください。

さらに、あなたはそれを仮定しているのであなたはNullPointerExceptionあなたのブロックにいます、そして両方ともnullではありません:その仮定をしないでください。閉じる前に確認してください。finallyinputBufferinput

于 2012-12-16T19:23:52.257 に答える
2

ファイルは抽象的なパスです。これを実行する:

File f = new File("Data.txt");

ディスク上では絶対に何もしません。これもそうではありません

System.out.println(f.getAbsolutePath());

ファイルの存在のテスト。

これを行う:

if(file.exists()) {
    // yes it's there
}
于 2012-12-16T19:24:02.440 に答える
1

他の人があなたがファイルを作成しないと述べたように、touch()ここからメソッドを試してください:ApacheFileUtils

于 2012-12-16T19:25:54.237 に答える
1

私はこれがうまくいくことを願っています:

この行を置き換えます

Repository repo = new Repository("Data.txt", gd);

と :

Repository repo = new Repository(f, gd);

そしてあなたの中で

public Repository (String fileName, GenericDictionary gd) 
                                           throws IOException, 
                                                  NumberFormatException, 
                                                  FileNotFoundException

これを使って

public Repository (File f, GenericDictionary gd) 
                                           throws IOException, 
                                                  NumberFormatException, 
                                                  FileNotFoundException

そして、代わりに{}を試してください

input = new FileReader(this.fileName);

これを行う

input = new FileReader(f.getAbsolutePath());
于 2012-12-16T19:27:51.417 に答える