Loading .txt files from assets

Ive been trying to load a txt file from my assets. It has information about my scenes so I can add levels without touching code.

Ill then use a scanner to run through the txt file and find info

So after serching around it looks like I might need to register a custom loader on the assetManager.

Is this the best way to load a txt file? How do I go about making the custom loader class?



Help appreciated.

Id be happy to make a wiki page about this if it would be benificial, since i couldnt find any pages on this.

Hi,



I know it is probably not the official Jme way to do it, but it is a good java way to do it so it’s fine with me.



[java]

InputStream inp = getClass().getResourceAsStream("/Textures/Debug/Debug.txt");

in = new BufferedReader(new InputStreamReader(inp));



while ((str = in.readLine()) != null) {

//do your thing

}

[/java]



Where the resource is a folder / file in the assets directory.

My other solutions to get this working didn’t work after compiling, this one does.



I’m sure somebody will respond to this and shows us the proper way to do this, but this works for me so , success!

1 Like

Why don’t you just add the levels in a separate class instead of a dodgy txt file? Not sure, but loading it from a txt sounds prone to errors.

1 Like

It sounds like you want to serialize and deserialize data into objects. I think JME already has a method called writeTo ? (I’m not sure). You should probably look into using that. If you find you don’t want to use that, google java serialization because using a txt file will probably cause you a lot of extra work and headache. If you are just looking for a settings file looking in the java properties API

If you put these in your assets folder, your not going to be able to add levels to it after the game is released (i think). Instead create a separate folder, and put your files in there, then this can be easily modified. I used java file serialization for storing levels of my first game, but it depends on the type and nature of the information your storing

1 Like

I don’t think a text file is really a resource from the JME point of view (since the JME3 system will have no idea or interest what to do with it). Just load and parse it using the standard java mechanisms for doing so.



I’d recommend using something a bit more structured than just pure text for something like that BTW. Something that can be validated and has editors/parses already available like XML. (I wouldn’t recommend XML for many things but something like this that only needs parsing once and needs to be both human and machine readable it’s ideal for).

@wezrule

after breif testing was able add levels without the source code. I edited the assests.jar and I could change start position and add more scenes fine.



@javagame & scrubalub

you are right that a seperate serializable class whould be more secure/reliable, but easy access is what im wanting



@dansion

Your method seems to be working perfectly. Thanks very much

OK, good to hear you got it working, but in my opinion an object would provide much easier access (you can do it whatever way you want, just saying) Also, zarch is right, xml was built for things like this.

I suggest xstream, you can just store and load objects with it.

Nice to hear you got it working, also you don’t have to edit the assets.jar directly.

If you use the SDK you can add/edit/delete the text-files directly in the projects assets in the projects explorer.

the are included in the assets.jar on compile and are loaded directly from the .txt when debugging.

How about doing it the proper, “jME3” style way? Integrate with the AssetManager.

[java]public class TextLoader implements AssetLoader {

public Object load(AssetInfo assetInfo) throws IOException {

Scanner scan = new Scanner(assetInfo.openStream());

StringBuilder sb = new StringBuilder();

try {

while (scan.hasNextLine()) {

sb.append(scan.nextLine()).append(‘n’);

}

} finally {

scan.close();

}

return sb.toString();

}

}[/java]



This will print out the text file located in the asset “Text/Blah.txt”

[java]assetManager.registerLoader(TextLoader.class, “txt”);

System.out.println(assetManager.loadAsset(“Text/Blah.txt”));[/java]

8 Likes

Yay, like I said :


I’m sure somebody will respond to this and shows us the proper way to do this....


Thanks for the example Momoko_Fan

Just a small note, the ‘n’ in that example is actually supposed to be new line (forward slash with the letter n following it). The forum doesn’t like them for some reason.

If you use Java7 you can also do it the way I do.



I need to have an asset loader for text files because I use the content of one as a name generator. The file contains text of possible arrangements for names.



[java]

/**

*

  • @author MadJack
  • @creation Nov 27, 2011 10:58:40 AM

    */

    public class TextLoader implements AssetLoader {



    @Override

    public Object load(AssetInfo assetInfo) throws IOException {



    ArrayList<String> dictionary = null;

    InputStream in = assetInfo.openStream();

    if (in != null) {

    dictionary = new ArrayList<>();



    try (BufferedReader bufRead = new BufferedReader(new InputStreamReader(in))) {

    String line = bufRead.readLine();

    while (line != null) {

    dictionary.add(line);

    line = bufRead.readLine();

    }

    }

    }

    return dictionary;

    }

    }

    [/java]



    And you use it the normal way (Beware! The following is only with the new changes made today!)

    [java]

    am.registerLoader(TextLoader.class, TextLoader.class.getName(), "dict");

    ArrayList<String> input = (ArrayList<String>) am.loadAsset(new TextFileKey("Dictionary/" + dictFileName));

    [/java]
2 Likes

Well, I just learned a new way to load my script files =]

I can’t make it work. :< What imports do I need?

Usually your IDE should tell /suggest you.

[java]
package mygame;

import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import java.io.IOException;
import java.util.Scanner;

/**

  • test
  • @author normenhansen
    */
    public class TextLoader implements AssetLoader {
    public Object load(AssetInfo assetInfo) throws IOException {
    Scanner scan = new Scanner(assetInfo.openStream());
    StringBuilder sb = new StringBuilder();
    try {
    while (scan.hasNextLine()) {
    sb.append(scan.nextLine()).append(‘n’);
    }
    } finally {
    scan.close();
    }
    return sb.toString();
    }
    }
    [/java]
    This doesn’t work.

“Doesn’t work” could mean about one of a thousand things so I will pick one at random to respond to:
“Back away from your computer and immediately grab a fire extinguisher. Do not use water to try and put out an electrical fire.”

Can someone paste example code with imports etc. ?