下面是我在Android開發中,一個寫文本文件的方法,代碼如下:
//將字符串寫入到文本文件中
public static void WriteTxtFile(String strcontent,String strFilePath)
{
//每次寫入時,都換行寫
String strContent=strcontent+"\n";
try {
File file = new File(strFilePath);
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + strFilePath);
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.write(strContent.getBytes());
raf.close();
} catch (Exception e) {
Log.e("TestFile", "Error on write File.");
}
}