在Android java中读取文本文件或XML文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2187567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:31:55  来源:igfitidea点击:

Reading a text file or XML file in Android java

javaxmlandroidfile

提问by Matthew

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).

我目前正在开发一个团队项目来构建一个 Android 游戏。我被委派了构建基于 PC 的关卡编辑器的任务(已完成)。

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; E.G 0=path 1=wall 2=enemy.

我现在要做的就是决定输出的地图的文件格式。目前它只是一个标准的文本文件,用不同的数字来代表不同的瓷砖;EG 0=路径 1=墙 2=敌人。

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.

实际的 Android 游戏然后获取此文件并将值存储到 2D 数组中。然而,那些制作实际游戏的人在打开文本文件时遇到了困难,其他人告诉我必须使用 XML 文件来完成。我现在被要求找一个地方来接受这个输入。

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

有人能告诉我如何读取文本文件并将值放入int array[][] 中吗?或者,如果更简单,请使用 XML 文件并告诉我如何格式化该文件并将值读入int数组 [][]

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

任何帮助将不胜感激,因为我没有学习如何使用 Android,所以我觉得这很困难。

Many thanks/

非常感谢/

回答by Macarse

I wouldn't use an xml for that.

我不会为此使用 xml。

I would choose between this two options: Check thisfirst.

我会在这两个选项之间进行选择:首先检查这个

Text File

文本文件

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

将文件添加到 /res/raw 并从那里读取它。

Hereyou 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.

您可能希望序列化所有内容int array[][]并将其保存在 /res/raw 中。

回答by jeffh

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.

查看有关使用 XmlResourceParser 解析自定义编译的 XML 的教程。这涉及创建放置在 /res/xml 中的自定义 XML 并使用 XmlResourceParser 解析自定义 XML。

Hope that helps.

希望有帮助。