`
q_wong
  • 浏览: 105622 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

監控異常記錄

阅读更多
/**
 * 維護監控異常記錄文件---TXT
 * 
 * 
 */
public class TxtFile implements IBaseFile {

	String dirName = "log";// 父目錄

	/*
	 * 匯出指定文件給用戶View (non-Javadoc)
	 * 
	 * @see com.htc.hr.salary.service.IBaseFile#loadFile(java.lang.String)
	 */
	public List<Object> loadFile(String source) throws Exception {
		// TODO Auto-generated method stub
		List<Object> exceptionList = new ArrayList<Object>();
		try {
			String tempStr;
			BufferedReader reader = new BufferedReader(new FileReader("../"
					+ dirName + "/" + source + ".txt"));
			while ((tempStr = reader.readLine()) != null) {
				exceptionList.add(tempStr);
			}
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
			throw new FileNotFoundException("文件沒有找到");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new IOException("文件操作異常");
		}
		return exceptionList;
	}

	/*
	 * 寫入錯誤信息到指定路徑 (non-Javadoc)
	 * 
	 * @see com.htc.hr.salary.service.IBaseFile#writeFile(java.util.List,
	 * java.lang.String)
	 */
	public boolean writeFile(List<Object> objectList, String source)
			throws IOException {
		// TODO Auto-generated method stub
		File dir = new File("../" + dirName);
		if (!dir.exists()) {//如果指定資料夾不存在則創建
			dir.mkdir();
		}
		
		//需要覆蓋日誌使用此段代碼,註釋掉下面c...d的代碼
		//a
		/*BufferedWriter writer = new BufferedWriter(new FileWriter("../"
				+ dirName + "/" + source + ".txt"));
		for (Object str : objectList) {
			writer.write(str.toString());
			writer.newLine();
		}
		writer.flush();
		writer.close();*/
		//b
		
		//不需要覆蓋日誌使用此段代碼,註釋掉上面a...b的代碼
		//c
		FileOutputStream fos = new FileOutputStream("../"
				+ dirName + "/" + source + ".txt",true);
		for (Object str : objectList) {
			fos.write(((String) str + "\n").getBytes());
		}
		fos.flush();
		fos.close();
		//d
		
		return true;
	}

}

 

	/**
	 * 查看薪資計算異常記錄
	 */
	public void viewExceptionRecords() {
		try {
			IBaseFile baseFile = baseFileFactory.createBaseFile("TXT");
			List<Object> tempList = baseFile.loadFile(exceptionFileName);
			StringBuffer sb = new StringBuffer();
			for (Object str : tempList) {
				sb.append(str);
				sb.append("\n");
			}
			this.setExceptionRecords(sb.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			FacesMessages.instance().add("沒有異常文件!");
		}
	}

 

// write exception into file
		try {
			if (exceptionList.size() != 0) {
				SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
				String fileName = format.format(payRollDate);
				IBaseFile baseFile = baseFileFactory.createBaseFile("TXT");
				baseFile.writeFile(exceptionList, fileName);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics