2

我希望我的应用程序在 sd 卡中创建一个文件夹并将输出文本文件放在那里,例如:mnt/sdcard/myfolder/textfile_name。我已经成功实现了getAbsolutePath的方法,但是我发现文本文件有问题,如果我在第一个文件中写5行然后在下一个文件中我写少于5行,第一个文件的最后一行会出现在第二个文件中。我在我的应用程序中使用附加命令。如何在不附加最后一个的情况下编写它。这些是我的代码

public void simpan(View v)
{
    SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yy_HH_mm_ss");
    String time = sdf.format(new Date());
    generate("Transaction " + time.toString() + ".txt");
    Toast.makeText(getApplicationContext(), "Record Saved to /mnt/sdCard/Soto Betawi/" + time.toString(), Toast.LENGTH_LONG).show();
}

private static void generate(String file_name)
{
    try
    {
        File root = Environment.getExternalStorageDirectory();
        File dir = new File(root.getAbsolutePath() + "/Soto Betawi");
        dir.mkdirs();
        File newfile = new File(dir, file_name);
        FileWriter writer = new FileWriter(newfile);
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
        String time = sdf.format(new Date());
        writer.append("Soto Betawi Asli\n");
        writer.append("Haji HUSEN\n");
        writer.append("Jalan Padang Panjang No. 6C\n");
        writer.append("Manggarai, Jakarta Selatan\n\n");
        writer.append("Transaction time : " + time + "\n");
        writer.append("-------------------------------------------------\n");
        writer.append("Menu\t\t\tPrice\t\t\tQuantity\n");
        writer.append("-------------------------------------------------\n");
        writer.append(value1 + "\t\t" + hrg1 + "\t\t" + qty1.getText().toString() + "\n");
        writer.append(value2 + "\t\t" + hrg2 + "\t\t"+ qty2.getText().toString() + "\n");
        writer.append(value3 + "\t\t" + hrg3 + "\t\t" + qty3.getText().toString() + "\n");
        writer.append(value4 + "\t\t" + hrg4 + "\t\t" + qty4.getText().toString() + "\n");
        writer.append(value5 + "\t\t" + hrg5 + "\t\t" + qty5.getText().toString() + "\n");
        writer.append(value6 + "\t\t" + hrg6 + "\t\t" + qty6.getText().toString() + "\n");
        writer.append(value7 + "\t\t" + hrg7 + "\t\t" + qty7.getText().toString() + "\n");
        writer.append(value8 + "\t\t" + hrg8 + "\t\t" + qty8.getText().toString() + "\n");
        writer.append(value9 + "\t\t" + hrg9 + "\t\t" + qty9.getText().toString() + "\n");
        writer.append(value10 + "\t\t" + hrg10 + "\t\t" + qty10.getText().toString() + "\n\n");
        writer.append("Total Pay : Rp. " + hasil);
        writer.flush();
        writer.close();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

谢谢。

4

3 回答 3

5
new FileWriter(newFile);

创建一个用于(覆盖)写入的文件。你需要使用

new FileWriter(newFile, true);

以附加模式打开它。

于 2012-12-28T17:00:17.087 回答
0

更改用于 FileWriter 的构造函数。

更改: FileWriter writer = new FileWriter(newfile); To: FileWriter writer = new FileWriter(newfile, true);

于 2012-12-28T17:00:32.423 回答
0
 FileWriter fWriter;
            try{
                 fWriter = new FileWriter(“\sdcard\yourfilename.txt”);
                 fWriter.write(yourString);
                 fWriter.flush();
                 fWriter.close();
             }catch(Exception e){
                      e.printStackTrace();
             }

flush() 和 close() 调用在这里很重要,还要确保挂载了“sdcard”

于 2012-12-28T17:54:14.133 回答