gpt4 book ai didi

android - android中的文件操作

转载 作者:太空狗 更新时间:2023-10-29 13:42:51 25 4
gpt4 key购买 nike

我是 android 平台的新手。我需要在 android 中创建一个文本文件。请让我知道如何在 android 中执行此任务。我写了一段代码,它在 java 中运行良好,但在 android 中却不行。请帮助我....我编写的示例代码是:-

try
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("test.txt", true));
dos.writeBytes(dataLine);
dos.close();
}
catch (FileNotFoundException ex) {}

上面的代码 fragment 在 java 中运行良好,但在 android 中运行不正常:(

谢谢,阿希什

最佳答案

Android 开发指南 explains it nicely :

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

如果您希望您创建的文件对外界可见,请使用 external storage .但正如我在评论中所说,确保你是“一个好公民”。即使在用户卸载您的应用后,这些文件仍然存在。

关于android - android中的文件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3758382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com