Java日志处理 一次写入一行字符


Java日志处理 一次写入一行字符

  1. /**  
  2.      * @param path文件地址  
  3.      * @param append是否追加  
  4.      * @param value写入值  
  5.      * @param charsetName字符集名称eg:[gb2312]  
  6.      * @throws IOException  
  7.      */  
  8.     public static void writerLine(String path,boolean append,String value,String charsetName) throws IOException   
  9.     {   
  10.         OutputStreamWriter osw=null;   
  11.         BufferedWriter bw=null;   
  12.         try {   
  13.             osw=new OutputStreamWriter(new FileOutputStream(path,append),charsetName);   
  14.             bw=new BufferedWriter(osw);   
  15.             bw.write(new Date().toLocaleString()+value);   
  16.             bw.newLine();   
  17.             bw.flush();   
  18.         } catch (UnsupportedEncodingException e) {   
  19.             System.out.println("没有指定的字符集");   
  20.             e.printStackTrace();   
  21.         } catch (FileNotFoundException e) {   
  22.             System.out.println("没有指定的文件");   
  23.             e.printStackTrace();   
  24.         } catch (IOException e) {   
  25.             e.printStackTrace();   
  26.             throw new IOException("IOException");   
  27.         }   
  28.         finally  
  29.         {   
  30.             bw.close();   
  31.             osw.close();   
  32.         }   
  33.     }  

相关内容