android - 如何在 Android 应用程序中将文本字段(用户输入)数据保存在文本文件中

标签 android file-io

我希望我在应用程序的编辑文本字段中编写的任何内容都会自动保存在 .txt 文件中。这样我就可以直接从那里检索该文件并通过一个按钮发送给任何人。 我的问题是如何直接保存为.txt格式。

最佳答案

当文本更改时,此代码 fragment 会将您的文本复制到字符串中,并调用一个函数来写入应用程序的私有(private)文件目录。

String YourTextString;    
    YourTextField.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
                YourTextString = YourTextField.getText().toString();
                writeToFile(YourTextString);
            }
            @Override
            public void afterTextChanged(Editable s) {      
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {         
            }
        });
public void writeToFile(String data) {
    try {
        OutputStreamWriter MyOutputStreamWriter = new OutputStreamWriter(mContext.openFileOutput("YourFileName.txt", Context.MODE_PRIVATE));
        MyOutputStreamWriter.append(data);
        MyOutputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

编辑: 如果你想从文件中读取,你可以使用这个函数(如果path参数为空,代码将从私有(private)文件目录中读取一个文件,如果你给它一个路径,它会读取你指定的文件):

public String readFromFile(String path) {
    String ret = "";
    if(path==""){
        try {
            InputStream inputStream = mContext.openFileInput(FileName);

            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString).append("\n");
                }

                inputStream.close();
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e("login activity", "Can not read file: " + e.toString());
        }
    }
    else
    {
        File file = new File(path);
        String receiveString = "";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader buf = new BufferedReader(new FileReader(file));
            while ( (receiveString = buf.readLine()) != null ) {
                stringBuilder.append(receiveString).append("\n");
            }
            buf.close();
            ret = stringBuilder.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return ret;
}

关于android - 如何在 Android 应用程序中将文本字段(用户输入)数据保存在文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734648/

相关文章:

android - Ant,错误膨胀类android.support.v7.widget.RecyclerView

java - 如何改变窗口表面的大小?

c - 从函数内打开文件

c++ - 在编译时读取文件(constexpr 或其他)

android - Jacoco 的覆盖率为 0%,而 Intellij Idea 的覆盖率非常低

android - 如何在android studio中编写平方根

java - setAdapter() 上的 NullPointer 异常

java - 了解何时使用 Buffer 处理 Java 中的文件,何时不使用

c - 在循环中使用 fscanf() 的问题

python - 还是关于python文件操作