<bdo id='XGdbU'></bdo><ul id='XGdbU'></ul>
  1. <tfoot id='XGdbU'></tfoot>

    <i id='XGdbU'><tr id='XGdbU'><dt id='XGdbU'><q id='XGdbU'><span id='XGdbU'><b id='XGdbU'><form id='XGdbU'><ins id='XGdbU'></ins><ul id='XGdbU'></ul><sub id='XGdbU'></sub></form><legend id='XGdbU'></legend><bdo id='XGdbU'><pre id='XGdbU'><center id='XGdbU'></center></pre></bdo></b><th id='XGdbU'></th></span></q></dt></tr></i><div id='XGdbU'><tfoot id='XGdbU'></tfoot><dl id='XGdbU'><fieldset id='XGdbU'></fieldset></dl></div>
  2. <legend id='XGdbU'><style id='XGdbU'><dir id='XGdbU'><q id='XGdbU'></q></dir></style></legend>

    <small id='XGdbU'></small><noframes id='XGdbU'>

      将格式化的字符串写入文件 - Java

      Writing a formatted string to a file - Java(将格式化的字符串写入文件 - Java)

      <small id='GPBvB'></small><noframes id='GPBvB'>

        <tbody id='GPBvB'></tbody>
      <legend id='GPBvB'><style id='GPBvB'><dir id='GPBvB'><q id='GPBvB'></q></dir></style></legend>

      <tfoot id='GPBvB'></tfoot>
      • <bdo id='GPBvB'></bdo><ul id='GPBvB'></ul>

                <i id='GPBvB'><tr id='GPBvB'><dt id='GPBvB'><q id='GPBvB'><span id='GPBvB'><b id='GPBvB'><form id='GPBvB'><ins id='GPBvB'></ins><ul id='GPBvB'></ul><sub id='GPBvB'></sub></form><legend id='GPBvB'></legend><bdo id='GPBvB'><pre id='GPBvB'><center id='GPBvB'></center></pre></bdo></b><th id='GPBvB'></th></span></q></dt></tr></i><div id='GPBvB'><tfoot id='GPBvB'></tfoot><dl id='GPBvB'><fieldset id='GPBvB'></fieldset></dl></div>
              1. 本文介绍了将格式化的字符串写入文件 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I have a string that I format with the System.out.format() method, I do something like :

                System.out.format("I = %3d  var = %9.6f", i, myVar);
                

                but when I try to write this formatted string into a file, I only get something like "java.io.PrintStream@84fc8d" in it.

                After looking into the documentation understood that this method is a little like System.out.print() and just return a PrintStream to display (in console for example), so I tried converting it with .toString or String.valueOf() but I get the same result.

                So I was wondering if there is a way to format a string just like the String.out.format() method does, but in a way that will be writable in a file ?

                Here is approximately the code I use (just making the useful parts appear)

                WRITE_MY_LINE(System.out.format(" I = %3d  var = %9.6f", i, myVar).toString());
                //also tried this :
                WRITE_MY_LINE(String.valueOf(System.out.format(" I = %3d  var = %9.6f", i, myVar)));
                
                public static void WRITE_MY_LINE(String line) {
                        buff_out = new BufferedWriter(new FileWriter(ascii_path, true));
                
                        buff_out.append(line);
                        buff_out.newLine();
                        buff_out.flush();
                }
                

                解决方案

                String.format is what you are looking for it returns a String and not a PrintStream like System.out.format.

                Your code should be:

                WRITE_MY_LINE(String.format(" I = %3d  var = %9.6f", i, myVar));
                

                Take a look at Java.lang.String.format() Method for further information.

                这篇关于将格式化的字符串写入文件 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
              2. <small id='634ru'></small><noframes id='634ru'>

                1. <legend id='634ru'><style id='634ru'><dir id='634ru'><q id='634ru'></q></dir></style></legend>

                      <tfoot id='634ru'></tfoot>
                          <tbody id='634ru'></tbody>
                      1. <i id='634ru'><tr id='634ru'><dt id='634ru'><q id='634ru'><span id='634ru'><b id='634ru'><form id='634ru'><ins id='634ru'></ins><ul id='634ru'></ul><sub id='634ru'></sub></form><legend id='634ru'></legend><bdo id='634ru'><pre id='634ru'><center id='634ru'></center></pre></bdo></b><th id='634ru'></th></span></q></dt></tr></i><div id='634ru'><tfoot id='634ru'></tfoot><dl id='634ru'><fieldset id='634ru'></fieldset></dl></div>
                          <bdo id='634ru'></bdo><ul id='634ru'></ul>