import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.*;public class Log {
  public Log() {
  }
  private static void wirteLog2File(String logMessage){
    File f=new File("Log.log");
    FileWriter fw=null;
      try {
        fw = new FileWriter(f,true);
      }
      catch (IOException ex1) {
        try {
          fw.close();
          ex1.printStackTrace();
        }
        catch (IOException ex2) {
        }      }
      try {
        fw.write(logMessage + "\n");
        fw.flush();
      }
      catch (IOException ex) {
        try {
          fw.close();
          ex.printStackTrace();
        }
        catch (IOException ex3) {
        }
      }
  }  public static void main(String [] args){
 Log.wirteLog2File("1、log message to file");
  Log.wirteLog2File("2、log message to file");
  }
}
//预期结果:
//1、log message to file
//2、log message to file
//实际结果:
//1、log message to file 黑框2、log message to file

解决方案 »

  1.   

    这个跟你看文件的编辑器有关的。有些编辑器认为\r\n是换行,特别是windows下的那些编辑器。有些则会对认\n,特别是那些用来编程的编辑器。所以,只写\n也并不是错的。
      

  2.   

    System.getProperty("line.separator");
    用这个取到当前系统的换行符。
      

  3.   

    UNIX 中 能识别 ‘\n’ 
    widows中 实现换行要用 '\r\n'才行‘\n'在windows中的识别 就是个黑框 但是并不错~就是原与他的识别问题
      

  4.   

    Please use Java Logging API or Apache log4j library.