我希望我的应用程序在 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();
}
}
谢谢。