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);