简体   繁体   English

创建txt文件Android时出错

[英]Error in creating txt file Android

I'm trying to create a file to save a flag which I'll need in further executions. 我正在尝试创建一个文件来保存我在后续执行中需要的标志。 This is the code which I wrote in order to create the file: 这是我为创建文件而编写的代码:

FileOutputStream fileout=openFileOutput("flag.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write("1");
outputWriter.close();

I detect the problem when I afterwards execute the app and try to read the file content, through the following code: 随后执行该应用程序并尝试通过以下代码读取文件内容时,我检测到该问题:

File f = new File("flag.txt");
if (f.exists())
   Toast.makeText(this, "The file exists", Toast.LENGTH_LONG).show();
else
   Toast.makeText(this, "The file wasn't created", Toast.LENGTH_LONG).show();

But it always follows the "else" branch. 但是它始终遵循“ else”分支。 Any ideas? 有任何想法吗?

要获取文件,请指定文件目录。

File file = new File(Environment.getExternalStorageDirectory(), "flag.txt");

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

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