0

So I am working on this game where I have a high score stored in a text file. I am following the newboston's tutorial on how to read and write to file. Right now I am testing whether the read class works and it is not working. It says cannot find file when I put the text exactly like how it is and the file is in my package so I don't have to say a path. Here is the method code in my game using both read and write classes:

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;
        }
    } 

Here is the read class:

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

Also here is the write file. I am concerned that this class might not work either as it looks like it maybe creating a new file and writing to that new one when I just want to write to an existing file I already have called "highScore.txt" Anyways here is the write class:

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

So I am wondering why it is not working and thanks in advance.

Bob G.
  • 143
  • 3
  • 14
  • Can you post the stacktrace, please? – Boris Feb 01 '18 at 17:49
  • To find out why you can't find the file use the methods `.exists()` and `getPath` or `getAbsolutePath` from the `File` class. see: https://docs.oracle.com/javase/7/docs/api/java/io/File.html consider to use `boolean` instead of `int` for your `change` variable in `getHighScore`. – Jérôme Feb 01 '18 at 17:51
  • How do I post the stacktrace? – Bob G. Feb 01 '18 at 17:58
  • Oh the stacktrace is this: Exception in thread "main" Could not find file. 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) at Game.main(Game.java:57) – Bob G. Feb 01 '18 at 17:59
  • Still having file not found issue, still persists if I try using the path. I tried the does file exist and it said false meaning it doesn't even though it does exist. – Bob G. Feb 01 '18 at 20:47

2 Answers2

0

If your file exists in the same location where ReadFile and WriteFile exists. Then you can use the below code.

URL url =   getClass().getResource("highScore.txt"); 
Scanner read=new Scanner(new File(url.getPath()));
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • But then how do you write something to that file? I just have 1 number on the first line in the text file. All I need write to do is write the new score over the old one so it is just the new score now. – Bob G. Feb 01 '18 at 18:00
  • Then consider using `Files` class: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html or an example http://www.adam-bien.com/roller/abien/entry/java_7_writing_a_string – Jérôme Feb 01 '18 at 18:02
  • Also your url example says file cannot be resolved to a type when I put the file name in there. – Bob G. Feb 01 '18 at 18:03
  • How come the new boston's method isn't working? What is an example of the docs site in use? – Bob G. Feb 01 '18 at 18:05
  • It looks like that doc site requires a path, I don't have a path since the file is in my project package along with all my pictures and ect. I do not have to have a path. – Bob G. Feb 01 '18 at 18:08
  • Bucky did not even have a path either since his file was in the package just like mine and his worked. – Bob G. Feb 01 '18 at 18:09
  • Can someone help as my it says my file does not exist, I even tried the path? This answer does not help. – Bob G. Feb 01 '18 at 20:45
0

Ok I solved it. The problem was wrong location and I still did not have to give a path. So basically I looked at this thread: The system cannot find the file specified in java

The part of the thread is listing the files in the directory. When I did that, I noticed everything it mentioned was in the project folder. I moved my file from the src folder to the project folder and it worked. So if you come across a problem where you know you spelled the file name right but get file does not exist, make sure that your file is in the project folder and NOT in the src folder.

Bob G.
  • 143
  • 3
  • 14