0

I have seen many topics on this subject, but non of them work as the resource cannot be in a static method, and I need a static method to make the getters work so I can actually load the world file in the world class.

it is different please read again, the Resource doesn't work as the method HAS to be static.

https://stackoverflow.com/questions/37257553/java-game-eclipse-cant-locate-txt-after-export-to-jar#=

Reading a resource file from within jar

Those had no answers so admins please read before saying the same thing to the others asking the same question. If I come out as a bit of an asshole, I apologize as I've been stuck on this problem, for the last few days trying to get it to fetch it from the resources instead of from the hard drive.

I have a world being created in the World file by using this line

world = new World(handler,"res/worlds/world1.txt");

it then renders the world using this funtion

loadWorld(path);

It then returns back a string to the Utilites

String file = Utilities.loadFileAsString(path);

Here is the utilites class, which converts all the spaces in the world file

public static String loadFileAsString(String path){
    StringBuilder builder = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while((line = br.readLine()) != null)
            builder.append(line + "\n");

        br.close();
    } catch(IOException e){
        e.printStackTrace();
    }

    return builder.toString();
}

It works fine in eclipse, but after it doesn't load the world file, because it doesn't fetch it via resources, my question is how would I go about doing this.

Community
  • 1
  • 1
BlueFoot
  • 15
  • 1
  • 5
  • You cannot use a `FileReader` with a path to read a resource within a JAR. This is said in the linked question. You have to use a `InputStream`. The fact that you need it in a static variable or not doesn't matter. – Tunaki Jun 29 '16 at 11:40
  • And also have you checked the jar file (e.g. with `7zip`) if it contains the world? – wake-0 Jun 29 '16 at 11:41
  • And if you had read my issue Tunaki, those didn't work, I've already explained it in my questions, so I'm not going to explain it a third or a forth time. Also yes it does Kevin. – BlueFoot Jun 29 '16 at 12:10
  • @Tunaki Also it does matter because the InputStreams don't like being put in try and catch statments, which I also need to make it replace the white space. – BlueFoot Jun 29 '16 at 12:23
  • 1
    The fact remains, you **cannot** use `FileReader` to read a file that is embedded in a JAR. What do you mean by *InputStreams don't like being put in try and catch statments* ? An `InputStream` is a class. `try-catch` is a statement, they have nothing in common. – Tunaki Jun 29 '16 at 12:34
  • @Tunaki In order to replace the FileReader I would need to replace the FileReader with InputStream in the try and catch method that I showed above. I cannot do that because the classes don't like try and catches. this line "BufferedReader br = new BufferedReader(new FileReader(path));" – BlueFoot Jun 29 '16 at 13:11
  • When I replace world = new World("res/worlds/world1.txt"); with the getClass().getResourceAsStream(("res/worlds/world1.txt"));it gets me to replace the Strings with InputStream, and I cannot do that as I need a string to remove the spaces in the file. the only one that I cannot replace with InputStream is the FileReader thats inside the try and catch. – BlueFoot Jun 29 '16 at 13:22

0 Answers0