java 寫txt文件 寫好之前是.tmp 寫好之後變成.txt

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// yyyyMMddHHmmssSSSXXXX
String fileNameString = sdf.format(date)
+ String.valueOf(Math.random() * 10000).subString(0, 4);
File fileDirPathFile = new File("G:/user/test/");
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("a");
sBuffer.append("b");

String txtNeiRong = sBuffer.toString();

if (!fileDirPathFile.exists()) {
fileDirPathFile.mkdirs();
}
String filePathAndName = "G:/user/test/yyyyMMddHHmmssSSSXXXX.tmp";
String filePathAndName1 = "G:/user/test/yyyyMMddHHmmssSSSXXXX.txt";
FileOutputStream fosFileOutputStream = new FileOutputStream(
filePathAndName);
OutputStreamWriter osw = new OutputStreamWriter(fosFileOutputStream,
"UTF-8");
osw.write(txtNeiRong);
osw.write("");
osw.close();
fosFileOutputStream.close();
File tmpFile = new File(filePathAndName);
File txtFile = new File(filePathAndName1);
tmpFile.renameTo(txtFile);


發佈了26 篇原創文章 · 獲贊 23 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章