0

I am new to java and I have a problem with finding a file I created that is usually visible within Eclipse.After running and refreshing multiple times I still don't see it.Here is my code, any help would be welcome.

    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();
    }
}
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185

3 Answers3

0

Try printing the absolute path.

File file = new File("highscore.txt");
System.out.println("file: " + file.getAbsolutePath());
Dele Taylor
  • 300
  • 5
  • 11
0

Thanks for help guys. It was some kind of weird bug and I solved it by restarting my PC. I have one other question though. My text file didn't store int correctly so I needed to change my score value to String. Why is that and what kind of file can I use that accepts both Strings and Integers?

-1

The file will be created where your class file is created. Ex - Say your file name path is "c:\dinesh\com\dinesh\MyFirstProgram.java". When you compile your file, you will have class file will be in "c:\dinesh\com\dinesh\MyFirstProgram.class". After the program runs, you will find the file in "c:\dinesh\com\dinesh\highscore.txt"

Dinesh Arora
  • 1,652
  • 2
  • 20
  • 27
  • OP specifically mentions being new to Java and using Eclipse. Eclipse will store class files in the {workspace}/{project}/bin/{package} folder assuming a standard Java Project setup. The working directory by default is the project folder not the folder in which the class file is stored. – D.B. Dec 09 '17 at 06:29