Java 异常处理, 来源: javaer 分享于 2025-03-31 点击 29131 次 点评:29 Java 异常处理, import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; public class ExceptionDefaultHandler { private final static String _relativeExceptionLogPath = "log"; private final static String _defaultExceptionLogFileName = "exception.log"; // private static boolean hasFile = false; private static void generateDefaultLogFile() { String absoluteFilePath = ProjectConfig.getNFSCAbsolutePath() + File.separator + _relativeExceptionLogPath + File.separator + _defaultExceptionLogFileName; File file = new File(absoluteFilePath); if(!file.exists()) { try { file.createNewFile(); } catch(IOException EX) { System.out.println(EX.toString()); } } } private static void generateLogFile(String fileName) { String absoluteFilePath = ProjectConfig.getNFSCAbsolutePath() + File.separator + _relativeExceptionLogPath + File.separator + fileName; File file = new File(absoluteFilePath); if(!file.exists()) { try { file.createNewFile(); } catch(IOException ex) { System.out.println(ex.toString()); } } } public static void handle(Exception ex, String fileName) { generateLogFile(fileName); String absoluteFilePath = ProjectConfig.getNFSCAbsolutePath() + File.separator + _relativeExceptionLogPath+ File.separator + fileName; File file = new File(absoluteFilePath); if(file.canWrite()) { boolean appendFlag = true; if(file.length() > 1000000) { appendFlag = false; } PrintWriter streamWriter = null; try { streamWriter = new PrintWriter(new FileOutputStream(file, appendFlag)); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(DateTimeUtil.getCurrentDateLocalFormatString()); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); ex.printStackTrace(streamWriter); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); streamWriter.close(); } catch(IOException EX) { System.out.println(EX.toString()); } finally { streamWriter.close(); } } else { System.out.println("You do not have the permission to write into the exception log file"); } } public static void handle(Exception ex) { generateDefaultLogFile(); String absoluteFilePath = ProjectConfig.getNFSCAbsolutePath() + File.separator + _relativeExceptionLogPath + File.separator + _defaultExceptionLogFileName; File file = new File(absoluteFilePath); if(file.canWrite()) { boolean appendFlag = true; if(file.length() > 1000000) { appendFlag = false; } PrintWriter streamWriter = null; try { streamWriter = new PrintWriter(new FileOutputStream(file, appendFlag)); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(DateTimeUtil.getCurrentDateLocalFormatString()); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); ex.printStackTrace(streamWriter); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); streamWriter.close(); } catch(IOException e) { System.out.println(e.toString()); } finally { streamWriter.close(); } } else { System.out.println("You do not have the permission to write into the exception log file"); } } } 相关文章暂无相关文章
用户点评