我需要一个示例来展示如何读取和写入文件,包括 Android 内部存储的路径
我已经厌倦了胡闹。 我开始相信这实际上是不可能的。 我在哪里可以找到一个显示如何编写文件“myPath/myFile.txt”的简单示例? 然后再读一遍?
这是我无法工作的代码块的示例:
if(pathExists(path, ctx))
{
File file = new File(ctx.getFilesDir().getAbsolutePath() +"/" + path, fileName);
FileInputStream fIn = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fIn);
StringWriter writer = new StringWriter();
IOUtils.copy(fIn, writer, "UTF-8");
fileStr = writer.toString();
}
这是我得到的错误:
“java.io.IOException:是一个目录”
I'm tired messing around.
And i'm beginning to believe that it actually isn't possible.
Where do i find a simple example showing how to write a file "myPath/myFile.txt"?
And then reading it back again?
This an example of a code block that i can't get to work:
if(pathExists(path, ctx))
{
File file = new File(ctx.getFilesDir().getAbsolutePath() +"/" + path, fileName);
FileInputStream fIn = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fIn);
StringWriter writer = new StringWriter();
IOUtils.copy(fIn, writer, "UTF-8");
fileStr = writer.toString();
}
This is the error that i get:
"java.io.IOException: Is a directory"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下代码片段会将应用程序空间中保存的文件复制到您所需的路径。
This following snippet will copy you files which are saved in the application space to your desired path.
除了 Android 系统分配给您自己的应用程序的私有文件空间之外,您无法写入 Android 内部存储(除非您的设备已 root)。
You could not write to Android internal storage (not unless your device is rooted) aside from the private file space assigned to your own application by the android system.
你必须像java中那样使用序列化方法。
要将文本保存为文件,您应该使用 FileOutputStream,而要读取文件,您应该使用 FileInputStream。您可以检查以下代码,它有一个简单的编辑文本和两个按钮,一个用于保存,一个用于读取该文件中保存的数据。
以下代码是将文本保存在名为 raksi.txt 的文件中。
以下代码是按钮获取的点击监听器。它从文件中读取数据并显示为 toast,
you have to use serialization methods as in java.
to save an text as file you should be using FileOutputStream and to read the file you should be using FileInputStream. You can check the following code it has a simple edittext and two buttons one to save and one to read data saved in that file.
The following code is to save text in the file named raksi.
The following code is the click listener for the button get. it reads the data from the file and displays as toast,