/** * 日志文件的写入 * @param contects */ private void saveLogsToFile(String contects){ String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); String rootPath = path.substring(0, path.lastIndexOf("/WEB-INF")); String folderpath=rootPath+"//userOPerationLogs"; File folder=new File(folderpath); if(!folder.exists()){ folder.mkdir(); } if(!folder.exists()) return; String day=PublicUtil.getYYYYMMDD(new Date()); String fileName=folderpath+"//longs_"+day+".txt"; System.out.println(fileName); FileWriter fw = null; try { //如果文件存在,则追加内容;如果文件不存在,则创建文件 File f=new File(fileName); fw = new FileWriter(f, true); }catch (IOException e) { e.printStackTrace(); } PrintWriter pw = new PrintWriter(fw); pw.println(contects); pw.flush(); try { fw.flush(); pw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } }