求助,為什么寫檔案的時候第一行總會多一行空行
private int createFile() {
SimpleDateFormat querydate = new SimpleDateFormat("yyyy-MM-dd");
Calendar startDT = Calendar.getInstance();
startDT.add(Calendar.DATE, -2);
String yesterday = querydate.format(startDT.getTime());
CheckFileLine checkdata = new CheckFileLine();
try {//資料庫連接例外直接捕獲,釋放記憶體連接
Connection cn = JDBCUtil.getConnection();
String sql = "select b.userType, b.userID, b.costType, s.TXNAMT, b.heater_amt, to_char(b.finish_time,'yyyymmddhh24miss') as ftime, s.paymentFlowNo" +
" from billinfo b,RECON_INFO s where s.RECON_DATE = '"+yesterday+"' and s.PROJECT_ID = '213159721' and s.BILL_ID_THIRDSYS=b.BILLID_THIRDSYS and s.RECON_STATUS = '02' and s.SPLIT_STATUS = '02' and s.IS_PAYBACK = '00'";
logger.info(sql);
PreparedStatement ps = cn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
SimpleDateFormat namedate = new SimpleDateFormat("yyyyMMddHH");
filename = namedate.format(new Date())+".txt";
FileUtil.fileLinesWrite(sc.getRealPath("/")+"checkfile/"+filename, "");
int count = 0;
while(rs.next()) {
checkdata.setUserType(rs.getString("userType"));
checkdata.setUserID(rs.getString("userID"));
checkdata.setCostType(rs.getString("costType"));
//checkdata.setCostPeroid(rs.getString("bill_date"));//為空
checkdata.setPayAmount1(rs.getDouble("TXNAMT"));
checkdata.setPayAmount2(rs.getDouble("heater_amt"));
checkdata.setPayDate(rs.getString("ftime"));
checkdata.setSerialID(rs.getString("paymentFlowNo").substring(3,23));
String aline = new String(checkdata.pack("GBK"), "GBK");
// String aline = new String(checkdata.pack(ParamConfig.UTF_8), ParamConfig.UTF_8);
FileUtil.fileLinesWrite(sc.getRealPath("/")+"checkfile/"+filename, aline);
count++;
}
SimpleDateFormat date2 = new SimpleDateFormat("yyyyMMdd");
String datestr = date2.format(startDT.getTime());
startTime = datestr+"000000";
endTime = datestr+"235959";
return count;
}catch(Exception e) {
e.printStackTrace();
}
return 0;
}
public static String fileLinesWrite(String filePath, String content){
String filedo = "write";
FileWriter fw = null;
try {
File file=new File(filePath);
//如果檔案夾不存在,則創建檔案夾
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
if(!file.exists()){//如果檔案不存在,則創建檔案,寫入第一行內容
file.createNewFile();
fw = new FileWriter(file);
filedo = "create";
}else{//如果檔案存在,則追加內容
fw = new FileWriter(file, true);
}
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
return filedo;
}
uj5u.com熱心網友回復:
這是從資料庫往檔案中寫?哪你判斷寫入的內容是否包含換行轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/100745.html
標籤:Java相關
上一篇:Luke 8.0 windows 點開之后出現的問題
下一篇:spring security
