简体   繁体   English

Android写入文本文件

[英]Android writing to a text file

I have a text file within my raw folder of my app that I intend to use as a simple way to save settings and then read them back when needed. 我的应用程序的原始文件夹中有一个文本文件,我打算将其用作保存设置,然后在需要时读回它们的一种简单方法。 I can read from this file with using the BufferedReader and what comes with it, but I've tried a few different ways to be able to write to this file and none seem to work. 我可以使用BufferedReader及其附带的内容从该文件​​中读取内容,但是我尝试了几种不同的方法来写入该文件,但似乎都无法正常工作。

It seems to me that the problem is I never actually get the file, and I assume this is simply because I don't exactly know how I am supposed to give it the correct directory and file name. 在我看来,问题是我从未真正获得文件,我认为这仅仅是因为我不完全知道如何为它提供正确的目录和文件名。 I've tried all I could come up with, and I tend to get errors like "No such file or directory exists" or "Read-only file system". 我已经尝试了所有可能的方法,并且倾向于出现诸如“不存在这样的文件或目录”或“只读文件系统”之类的错误。

This seems to be a very simple problem relating to me just giving the wrong information, so if anybody could point me in the right direction it would be much appreciated. 与我提供错误的信息有关,这似乎是一个非常简单的问题,因此,如果有人可以向我指出正确的方向,将不胜感激。

Thank you, Raphy 谢谢拉菲

For saving settings you should use SharedPreferences rather than coming up with a custom solution. 为了保存设置,您应该使用SharedPreferences而不是提供自定义解决方案。

I'm not entirely sure you can do that with stuff in the "raw" folder. 我不完全确定您可以使用“原始”文件夹中的内容来做到这一点。

One approach would be to use the SharedPreference storage in the API. 一种方法是在API中使用SharedPreference存储。 See in the datastorage section of the docs. 请参阅文档的数据存储部分。 It's perfect from what you describe neededing. 根据您的描述,它是完美的。 Another approach would be to put the file on the SDCard and read and write it from there. 另一种方法是将文件放在SDCard上并从那里读写。

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

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