为什么文件被android外部存储器覆盖?

时间:2013-09-03 18:41:01

标签: android

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

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

我做错了什么?我正在使用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();
    }
}

2 个答案:

答案 0 :(得分: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);
    }

}

我希望这会对你有所帮助。

答案 1 :(得分: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();
    }