0

我正在尝试将学生 ID 写入 sdcard 的文本文件中。我已经students.txt在我的 sdcard 中创建了一个名为的文件。

问题是当我写一个值并读取文件时,我只得到文件中的最后一个值。当我打开我的文件时,我只能看到其中写入的最后一个值。

我究竟做错了什么?我正在使用追加模式写入文件,outputStreamWriter但问题仍然存在。

    //function to insert
private void myInsertFunc(String data) {
    try {
        File file = new File("/sdcard/" + FILENAME);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
        outputStreamWriter.append(data);

        outputStreamWriter.close();
        fileOutputStream.close();

        Toast.makeText(getApplicationContext(), "Student ID: " + data + " Inserted Successfully!", Toast.LENGTH_SHORT).show();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

//function to read
private void myReadFunc() {
    try {
            //InputStream inputStream = openFileInput(FILENAME);
        //InputStream inputStream = getResources().openRawResource(R.raw.student);
        File file = new File("/sdcard/" + FILENAME);
        FileInputStream fileInputStream = new FileInputStream(file);

        if (fileInputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            while ((receiveString = bufferedReader.readLine()) != null) {
                //if(receiveString.contains(data))
                //{
                    Toast.makeText(getApplicationContext(), "Student ID: " + receiveString , Toast.LENGTH_SHORT).show();
                    //break;
                //}
            }

            bufferedReader.close();
            inputStreamReader.close();
            fileInputStream.close();
        }
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
4

2 回答 2

1

我对 FileOutputStream 和其他人有同样的问题。写作时,他们总是在创建一个新文件。FileWriter为我解决了这个问题。应该看起来像这样:

private void myInsertFunc(String data) {
    //opens andor creates the file students.txt in the folder /storage/emulated/0/Android/data/<your package name>/files/
    File file = new File(ctxt.getExternalFilesDir(null), "students.txt");
    try {
        //create a filewriter and set append modus to true
        FileWriter fw = new FileWriter(file, true);
        fw.append(data);
        fw.close();

    } catch (IOException e) {
        Log.w("ExternalStorage", "Error writing " + file, e);
    }

}

我希望这可以帮助你。

于 2013-11-12T17:00:06.243 回答
0

就像是:

    File outFile = new File(Environment.getExternalStorageDirectory() ,FILENAME);

    try {

        FileWriter fw = new FileWriter(outFile, true);

        try{
          fw.write("Hello, world!");
        }finally {
          fw.close();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
于 2013-09-03T19:16:31.520 回答