2

したがって、タイトルは私の問題をほぼ要約していますが、コードに関する限り、何が間違っているのかわかりません。

これが私がファイルに書き込むスニップビットです:

                try {

                    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                    OutputStreamWriter osw = new OutputStreamWriter(fos);

                    osw.append(assignmentTitle + "\n" + assignmentDate + "\n");
                    osw.flush();
                    osw.close();

                } catch (FileNotFoundException e) {
                    //catch errors opening file
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

編集:これは、アクティビティが呼び出されるたびにファイルから読み取る場所です

private void readDataFromFile() {
        try {
            //Opens a file based on the file name stored in FILENAME.
            FileInputStream myIn = openFileInput(FILENAME);

            //Initializes readers to read the file.
            InputStreamReader inputReader = new InputStreamReader(myIn);
            BufferedReader BR = new BufferedReader(inputReader);

            //Holds a line from the text file.
            String line;

            //currentAssignment to add to the list
            Assignment currentAssignment = new Assignment();

                while ((line = BR.readLine()) != null) {
                    switch (index) {
                    case 0:
                        //Toast.makeText(this, line, Toast.LENGTH_LONG).show();
                        currentAssignment.setTitle(line);
                        index++;
                        break;
                    case 1:
                        //Toast.makeText(this, Integer.toString(assignmentListIndex), Toast.LENGTH_LONG).show();
                        currentAssignment.setDate_due(line);
                        Statics.assignmentList.add(assignmentListIndex, currentAssignment);
                        index = 0;
                        assignmentListIndex++;
                        currentAssignment = new Assignment();
                        break;
                    default:
                        Toast.makeText(this, "error has occured", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
                BR.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

これは、ユーザーが[新しい割り当ての作成]をクリックしたときの関数です。割り当ての[保存]ボタンをクリックすると、その割り当てがファイルに保存され、後でそれを読み取ってlistViewに表示することになっています。それが行っているのは、listViewの最初の項目を表示することであり、新しい割り当てを作成すると、保存ファイルの前のテキストが上書きされ、listViewで置き換えられます。あなたたちが私にもっとコードを投稿する必要があるなら私に知らせてください。なぜこれが機能しないのか混乱しています:(

4

2 に答える 2

10

の代わりにContext.MODE_PRIVATE、を使用しますContext.MODE_APPEND。このモードは、既存のファイルを消去するのではなく、追加します。(ドキュメントにあるopenFileOutputものの詳細。)

于 2012-09-09T03:00:58.603 に答える
0

クラスを使用する代わりに、次のようにクラスOutputStreamWriterを使用することをお勧めします。BufferedWriter

private File myFile = null; 
private BufferedWriter buff = null;

myFile = new File ( "abc.txt" );

buff = new BufferedWriter ( new FileWriter ( myFile,true ) );

buff.append ( assignmentTitle );
buff.newLine ( );
buff.append ( assignmentDate ); 
buff.newLine ( );
buff.close();
myFile.close();
于 2012-09-09T03:19:11.203 に答える