【问题标题】:New line writing to a file写入文件的新行
【发布时间】:2011-06-23 09:00:58
【问题描述】:

我正在尝试将我的应用程序日志写入文件。我创建了一个在行尾追加一行的方法。

    public static void write2Log(String s){
    StringBuilder contents = new StringBuilder();
    File root = Environment.getExternalStorageDirectory();
    File logFile = new File(root, "testWrite2file.log");

    if (root.canWrite()){               
        try {
            BufferedReader input =  new BufferedReader(new FileReader(logFile));
            String line = null;                            
            while (( line = input.readLine()) != null){
              contents.append(line);
             // contents.append(System.getProperty("line.separator"));            
        }
        input.close();
        FileWriter logWriter = new FileWriter(logFile);                  
        BufferedWriter out = new BufferedWriter(logWriter); 
        out.write(contents.toString()+"\r\n"+s);////<---HERE IS MY QUESTION
        out.close();
        }

        catch (IOException e) {
            Log.e("test", "Could not read/write file " + e.getMessage());
        }
    }
}

除了新的字符行之外,一切都很好。

我尝试过使用:

\r\n

System.getProperty("line.separator")

newline()

但我一直在一行中得到所有内容:(

有什么想法吗?

【问题讨论】:

  • logWriter.newLine() 为我工作。 BufferedWriter 的 newLine() 也应该可以工作。奇怪
  • 你试过的 newline() 方法是什么? System.getProperty("line.separator") 应该可以解决问题。 \t 会给你标签吗?如果不是,那么还有其他问题。确保您实际上正在运行该代码。将所有内容包装在一个字符串中,在写入文件之前在终端/调试屏幕上输出字符串。至少它应该在调试屏幕上工作
  • 你知道Log吗?
  • 嗨 Felix,是的,我可以在模拟器中使用日志。我可以使用日志写入文件吗?
  • this can be of help 挖掘上述项目的源代码。你会在那里的某个地方得到你想要做的事情

标签: android filewriter


【解决方案1】:

试试这个:

FileWriter logWriter = new FileWriter(logFile);                  
BufferedWriter out = new BufferedWriter(logWriter); 
out.write(contents.toString());////<---HERE IS THE CHANGE
out.newLine();
out.write(s);
out.close();

【讨论】:

    【解决方案2】:

    试试这个

    public static boolean writeLog(String  user , Exception exS) {
    
    
    
        boolean d = false;
    
        try {
    
            File root = new File("TVS_log");
            if (!root.exists()) {
                root.mkdirs();
            }
            String name = user + "_" + TimeReporter.getTodayAll() + "_" + "log.txt" ;
            System.out.println(name);
            File text = new File(root ,  name);
            if (!text.exists()) {
                text.createNewFile();
            }
    
            String aggregate = "";
            for (StackTraceElement element : exS.getStackTrace())
            {
                BufferedWriter writer = new BufferedWriter(new FileWriter(text)) ;
                String  message =  exS.toString() + " - " +   element.toString() + "\r\n" ; 
                System.out.println(exS.toString());
                System.out.println(element.toString());
                aggregate += message;
                writer.write (aggregate);
                writer.newLine();
                writer.flush();
                writer.close();
                writer = null;
            }                                                                
            if(text.exists())
                return text.length() > 0;                                   
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return d;
    
    }
    

    TimeReporter 类和函数

    public class TimeReporter {
    
      public static String getTodaysDate() {
    
            String d = "";
            final Calendar c = Calendar.getInstance();
            d = String.format("%02d", c.get(Calendar.YEAR))                 
                        + String.format("%02d", c.get(Calendar.MONTH) + 1)  
                            + String.format("%02d", c.get(Calendar.DAY_OF_MONTH));
            return d;
        }
    
        public static String getTodaysTime() {
    
            String d = "";
            final Calendar c = Calendar.getInstance();
            d =     String.format("%02d", c.get(Calendar.HOUR_OF_DAY))              
                        + String.format("%02d", c.get(Calendar.MINUTE))  
                            + String.format("%02d", c.get(Calendar.SECOND));
            return d;
        }
    
        public static String getTodayAll() {        
            return getTodaysDate() + "_" + getTodaysTime() ;
        }
    
    
    
    public static String getNow() {
        final Calendar c = Calendar.getInstance();
        String ld = c.get(Calendar.YEAR) + "/" + (c.get(Calendar.MONTH) + 1)
                + "/" + c.get(Calendar.DAY_OF_MONTH) + " "
                + String.format("%02d", c.get(Calendar.HOUR_OF_DAY)) + ":"
                + String.format("%02d", c.get(Calendar.MINUTE));
        return ld;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2014-05-10
      • 1970-01-01
      • 2021-07-19
      • 2011-12-17
      • 1970-01-01
      相关资源
      最近更新 更多