簡體   English   中英

找不到我創建的文件

[英]Can't find file I created

我是Java新手,我在查找通常在Eclipse中可見的文件時遇到問題。多次運行和刷新后仍然看不到它,這是我的代碼,歡迎任何幫助。

    Scanner input = new Scanner(System.in);
    Scanner x= new Scanner(System.in);
    System.out.println("You got a high score!");
    System.out.println();
    System.out.println("Please enter your score: ");

    int score = input.nextInt();
    System.out.println("Please enter your name: ");

    String name = x.nextLine();

    input.close();

    File file = new File("highscore.txt");

    try(BufferedWriter bw = new BufferedWriter(new FileWriter(file))){

        bw.write(score);
        bw.write(name);

    } catch (FileNotFoundException e) {
        System.out.println("File is not found");;
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

嘗試打印絕對路徑。

File file = new File("highscore.txt");
System.out.println("file: " + file.getAbsolutePath());

感謝您的幫助。 這是一種奇怪的錯誤,我通過重新啟動PC來解決了。 我還有一個問題。 我的文本文件未正確存儲int,因此我需要將得分值更改為String。 為什么會這樣?我可以使用哪種文件同時接受字符串和整數?

該文件將在創建類文件的位置創建。 例如-假設您的文件名路徑為“ c:\\ dinesh \\ com \\ dinesh \\ MyFirstProgram.java”。 當您編譯文件,你將有類文件將在“C:\\迪內希\\ COM \\迪內希\\ MyFirstProgram ”。 程序運行后,您將在“ c:\\ dinesh \\ com \\ dinesh \\ highscore.txt”中找到文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM