简体   繁体   中英

Reading a text file or XML file in Android java

I am currently working on a group project to build an Android game. I have been delegated the task of building a PC based level editor (which is all done).

All I have to do now is decide on the file format for the map that is outputted. At the moment it is just a standard text file with different numbers to represent different tiles; EG 0=path 1=wall 2=enemy.

1 1 1 1 1 1 1 1
1 0 0 0 2 0 0 1
1 1 1 1 1 1 1 1

The actual Android game then takes this file and stores the values into a 2D Array. However those who are making the actual game are having difficulty opening a text file, and others have told me it has to be done using an XML file. I have now been asked to find a away to take in this input.

Could someone tell me how I would read the text file and put the values into a int array[][] ? Or if it is easier, use an XML file and tell me how to format that file and read the values into a int array[][]

Any help would be appreciated as I didn't learn how to use Android so I find this quite difficult.

Many thanks/

I wouldn't use an xml for that.

I would choose between this two options: Check this first.

Text File

Add the file to /res/raw and read it from there.

Here you have an example code of how to read it.

Serialized structure

You might want to serialize all the int array[][] and save it in /res/raw.

Check out this tutorial on Using XmlResourceParser to Parse Custom Compiled XML . This involves creating custom XML placed in /res/xml and using XmlResourceParser to parse through the custom XML.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM