简体   繁体   English

将文本文件放在Android目录中的何处

[英]Where to put text files in directory in Android

I have a text file i want to include in my Android application, it is not a string file it is a standard text file.我有一个文本文件要包含在我的 Android 应用程序中,它不是字符串文件,而是标准文本文件。 It contains data that defines the characteristics of a "map" that is drawn on a board.它包含定义在板上绘制的“地图”特征的数据。 The file is not an XML file so i am unsure where i should put it or if this isn't good file structure for android?该文件不是 XML 文件,所以我不确定应该把它放在哪里,或者这是否不是 android 的良好文件结构? Are you suppose to do this?你想这样做吗? If you are then under what directory are you suppose to put them?如果你在什么目录下,你想把它们放在什么目录下? How then are you suppose to access the file?那么你想如何访问该文件? I know how to use FileInputStreams and FileOutputStreams i just need to know how to access the file.我知道如何使用 FileInputStreams 和 FileOutputStreams 我只需要知道如何访问文件。 All relevant answers are welcome and appreciated!欢迎和感谢所有相关的答案!

Use assets or raw folder in your android folders structure to keep that file.在您的 android 文件夹结构中使用assetsraw文件夹来保留该文件。 For more info read this有关更多信息,请阅读此内容

You have to put your file in the assets folder as Waqas said.正如 Waqas 所说,您必须将文件放在 assets 文件夹中。

Now to access it you do it like that.现在要访问它,您可以这样做。

I give you an example using BufferedReader我给你一个使用 BufferedReader 的例子

BufferedReader reader = new BufferedReader(
                 new InputStreamReader(getAssets().open("YourTextFile.txt")));

Be careful.当心。 In my case, I don't know why for now, I cannot read text files bigger than ~1MB and I had to split them in multiple small files.就我而言,我现在不知道为什么,我无法读取大于 ~1MB 的文本文件,我不得不将它们拆分为多个小文件。 It seems other had the same problem of file size but I didn't find any information about that on Android developer site.似乎其他人也有同样的文件大小问题,但我在 Android 开发者网站上没有找到任何相关信息。 If any one knows more about this ....如果有人对此了解更多......

FOLLOW UP跟进

My problem with the 1MB was due to a know bug/limitation of earlier versions of Android.我对 1MB 的问题是由于早期版本的 Android 的已知错误/限制。 Since using recent versions of Android, that problem is not present anymore.由于使用了最新版本的 Android,该问题不再存在。

I would just like to add to the accepted answer (I don't have enough reputation to comment unfortunately.) The link there to the tutorial that explains how to set up the res/raw method or the assets method is mostly good, but there's actually a MUCH easier way.我只想添加到已接受的答案中(不幸的是,我没有足够的声誉来发表评论。)那里的教程链接解释了如何设置 res/raw 方法或资产方法大多很好,但是有实际上是一种更简单的方法。 Look at the function described there called LoadFile.查看那里描述的名为 LoadFile 的函数。 That function is rather verbose.该函数相当冗长。 Lets say all you need is an InputStream variable so that you can read and write to a file.假设您只需要一个 InputStream 变量,以便您可以读取和写入文件。 Then delete everything after line 77!然后删除第 77 行之后的所有内容! Also you don't need the resource id at all!此外,您根本不需要资源 ID! You can use this function:您可以使用此功能:

//get the file as a stream
iS = resources.openRawResource(R.raw.mygpslog)

Now all you have to do is return iS and you will have your much desired file handle.现在您所要做的就是返回 iS,您将拥有您非常想要的文件句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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