简体   繁体   English

如何在 Android Studio 中将文本保存到文件?

[英]how to save text to file in Android Studio?

I want to create file in which the first line will be constantly updated.我想创建第一行将不断更新的文件。 actually I want to save this file to custom path, for example /storage/emulated/0/Download, but I don't know how to do that, so now I have something like this:实际上我想将这个文件保存到自定义路径,例如/storage/emulated/0/Download,但我不知道该怎么做,所以现在我有这样的东西:

public void save(){
    while(true) {
        try {
            String FILENAME = "my_file";
            String string = "" + System.currentTimeMillis();

            FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);
            fos.write(string.getBytes());
        } catch (Exception e) {

            e.printStackTrace();

        }
    }
}

and this code gives me two errors这段代码给了我两个错误

  1. Cannot resolve method 'openFileOutput'无法解析方法“openFileOutput”
  2. Cannot resolve symbol 'MODE_PRIVATE'无法解析符号“MODE_PRIVATE”
 FileOutputStream fos = getApplicationContext().getContextResolver().openFileOutput(FILENAME);

Before using openFileOutpout() you have to use getContextResolver() and there is no Context.MODE_PRIVATE parameter in this function在使用openFileOutpout()之前,您必须使用getContextResolver()并且此 function 中没有Context.MODE_PRIVATE参数

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

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