0

I am working on a basic game similar to Break Out and I plan to use a text file to store level data on where various objects should be located when a level is rendered onto the screen. Here is the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class LevelData {

public LevelData(){
    readFile();
}

public void readFile(){

    try{
        BufferedReader in = new BufferedReader(new FileReader("Levels"));
        String str;
        while((str = in.readLine()) != null){
            process(str);
        in.close();
        }
    }catch (IOException e){
        System.out.println("File Does Not Exist");
    }
}

private void process(String str){
    System.out.println(str);
}

}

This following code, based off of previous research, should access the file "Levels" that is located in the same package as the Java class, but if the program cannot access the file then it should print "File Does Not Exist" to the console. I have created a file called "Levels" that is located in the same package as the Java class but whenever I run this class, it does not read the file properly and it catches the Exception.

Does anyone have any ideas on why it cannot access the proper file? I have already looked on various other threads and have found nothing so far that could help me.

Thanks in advance

4
  • Are you using an IDE? Commented Jun 23, 2014 at 16:45
  • 1
    IS it in a folder, I find sometimes you're brought back to the project folder and have to navigate back. "/src/Levels.txt" Commented Jun 23, 2014 at 16:46
  • You could also have your program create the file, then see where it appears to get a sense of what working directory your program is using. Commented Jun 23, 2014 at 16:47
  • If the file is in your source package then you should load it as a Class Resource. If the file is in your project directory (next to your final jar file) then you should load it as a file as you do in the question. Commented Jun 23, 2014 at 16:51

6 Answers 6

1

Your Levels file probably doesn't want to be in the same package as the class, but rather, in the same directory from where your java program was run.

If you're using eclipse, this is probably the project directory.

Sign up to request clarification or add additional context in comments.

Comments

0

Your issue is probably the lack of a file extension!

BufferedReader in = new BufferedReader(new FileReader("Levels.txt"));

1 Comment

Only if they created the original file with an extension (it is possible to have an extensionless file, even on windows).
0
  1. In case you are running from Eclipse, the file should be accessed like new FileReader("src/<package>/Levels") .
  2. Closing the inputstream in.close(); should happen outside the while loop.

Comments

0

As you said you plan to use a text file to store level data. One way to solve the problem is to provide relative path to the file while storing and while reading the file use the relative path to read the file. Please don't forget to specify the extention of the file.

Comments

0

I see a few problems, the first one being there's no file extension. Second, the in.close() is in the while loop, and since you are planning on storing high scores, I would recommend you would use an ArrayList.

Comments

0

It is always a good practice to specify the absolute path name of the file if the file is external to your jar file. If it is part of your jar file, then you need to get it using 'getResourceAsStream()'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.