How to write files xml files on android and read them later

Hi,

My game wants to create xml files on disk in android and read them later. I have written a AssetLoader class for xml reading and it works flawlessly with xmls. However I have no idea how should I write files in classpath. Searching on google dont let any solutions that work. please point some directions.

This raise expection in android, works fine on pc

[java] try {
//InputStream in = this.getClass().getClassLoader().getResourceAsStream(“SomeTextFile.txt”);
FileOutputStream fileOut = new FileOutputStream("./assets/response" + uniqueId + “_” + qId + “.tio”);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(response);
out.close();
fileOut.close();

    } catch (FileNotFoundException e) {
        //e.printStackTrace();
        log.error("Error while dumping user response");
    } catch (IOException e) {
        log.error("Error while dumping user response");
        //e.printStackTrace();
    }

[/java]

The assets folder doesn’t exist anymore once your app has been packaged. You need to read and write your assets to a location that exists… and then register that location with the AssetManager.

The sample game I put together does this for storing highscores. You could download that and gank the code.

I think I set it up to test if the file exists, if it doesn’t, it loads a template xml file from the project jar and then creates a new file in the apk install directory. Seems to work just fine… and didn’t require an AssetLoader (for the obvious reasons).

@t0neg0d said: The sample game I put together does this for storing highscores. You could download that and gank the code.

I think I set it up to test if the file exists, if it doesn’t, it loads a template xml file from the project jar and then creates a new file in the apk install directory. Seems to work just fine… and didn’t require an AssetLoader (for the obvious reasons).

which is the sample game. where should i download it from?

@simar.i3r said: which is the sample game. where should i download it from?

Here

It’s the first in the list.

To run it, you’ll also need the gui library jar.

@pspeed said: The assets folder doesn't exist anymore once your app has been packaged. You need to read and write your assets to a location that exists... and then register that location with the AssetManager.

Almost all methods of assetManager including Assetmanager.locateAsset() requires an AssetKey. How to get Assetkey for an object??

new TextureKey(“gurke.jpg”);

MyXMLKey implements/extends AssetKey{
//stuff
}

I have tried many things in vain. I have messed with assetmanager, assetloader, keys, UrlLocator, FileLocator, ClassPathLoader but I dont know how to use them and cant find example code that suggest it.
I need spoonfeeding this time.
Kindly suggest how this can be done in some more detail…

Well first register a Fillocator with the rootpath where you xmls shall be.
Then you can use an assetkey to load get it via locateAsset.

For this you might need a custom assetkey.
You can just extend or implement (not sure if assetkkey is a class or interface atm) the assets then, by using a relative path below your given rootpath in the locator.

@Empire Phoenix said: Well first register a Fillocator with the rootpath where you xmls shall be. Then you can use an assetkey to load get it via locateAsset.

For this you might need a custom assetkey.
You can just extend or implement (not sure if assetkkey is a class or interface atm) the assets then, by using a relative path below your given rootpath in the locator.

How will I get an output stream from this??
AssetInfo has an getStream() method that returns an input stream.
i want to write new files…

new OutputStream(myrootfolder+"/path/+myfile.xml)

@Empire Phoenix said: new OutputStream(myrootfolder+"/path/+myfile.xml)

please excuse me, I’m missing big basics here but I’m still not able to manage it
How to find myrootfolder on android??

If I had rootpath what was the issue…
All attempts to take rootpath has resulted in vain for me
[java] am.loadAsset(“modules.xml”);
scorePath = getClass().getClassLoader().getResource(“modules.xml”).toString();
System.out.println(scorePath);
[/java]

also raises a FileNotFoundException in androidw which is fine as assets folder is not copied in andoird. I can load the xml using am.loadAsset(), how should I get output stream to write back???

@t0neg0d said: Here

It’s the first in the list.

To run it, you’ll also need the gui library jar.

I have downloaded the latest tonegod.gui.jar and adding it in the project does fix everything. There are some loose joystick related imports. I have not tested your game but I have checked its source. In highscores.java you have

		scorePath = getClass().getClassLoader().getResource(
			"mygame/scores.xml"
		).toString();

But when I add this code in my project, it still results in NullPointerException even after I carefully change the name to the file that exist. Please guide how should I do??

My requirement is simple store/create xml file anywhere in android where I can read them later…