写入文件的新行

发布于 2024-10-16 13:13:38 字数 1215 浏览 1 评论 0原文

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

    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()

但我总是在一行中获取所有内容:(

有什么想法吗?

I am trying to write to a file the log of my application. I have created an method which appends a line to the end of the line.

    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());
        }
    }
}

Everything works fine less the new character line.

I have tried using:

\r\n

System.getProperty("line.separator")

newline()

But i keep getting all in just one line :(

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-10-23 13:13:38

试试这个:

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();

Try this:

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();
梦言归人 2024-10-23 13:13:38

尝试这个

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;
}
}

Try this

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 class and function

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;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文