<bdo id='TgSmw'></bdo><ul id='TgSmw'></ul>
  • <i id='TgSmw'><tr id='TgSmw'><dt id='TgSmw'><q id='TgSmw'><span id='TgSmw'><b id='TgSmw'><form id='TgSmw'><ins id='TgSmw'></ins><ul id='TgSmw'></ul><sub id='TgSmw'></sub></form><legend id='TgSmw'></legend><bdo id='TgSmw'><pre id='TgSmw'><center id='TgSmw'></center></pre></bdo></b><th id='TgSmw'></th></span></q></dt></tr></i><div id='TgSmw'><tfoot id='TgSmw'></tfoot><dl id='TgSmw'><fieldset id='TgSmw'></fieldset></dl></div>
    <tfoot id='TgSmw'></tfoot>

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

      2. <small id='TgSmw'></small><noframes id='TgSmw'>

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

        时间:2023-09-29

            <tfoot id='jgID1'></tfoot>

            1. <small id='jgID1'></small><noframes id='jgID1'>

                <bdo id='jgID1'></bdo><ul id='jgID1'></ul>
              • <i id='jgID1'><tr id='jgID1'><dt id='jgID1'><q id='jgID1'><span id='jgID1'><b id='jgID1'><form id='jgID1'><ins id='jgID1'></ins><ul id='jgID1'></ul><sub id='jgID1'></sub></form><legend id='jgID1'></legend><bdo id='jgID1'><pre id='jgID1'><center id='jgID1'></center></pre></bdo></b><th id='jgID1'></th></span></q></dt></tr></i><div id='jgID1'><tfoot id='jgID1'></tfoot><dl id='jgID1'><fieldset id='jgID1'></fieldset></dl></div>
                  <tbody id='jgID1'></tbody>
                <legend id='jgID1'><style id='jgID1'><dir id='jgID1'><q id='jgID1'></q></dir></style></legend>
                  本文介绍了将格式化的字符串写入文件 - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MissingFormatArgumentException 错误 下一篇:带有 dateStyle 和 timeStyle 的 Joda-Time 格式化程序

                  相关文章

                  最新文章

                  1. <small id='de44i'></small><noframes id='de44i'>

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

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