我有一个用以下格式设置的字符串System.out.format()
方法,我做了类似的事情:
System.out.format("I = %3d var = %9.6f", i, myVar);
但是当我尝试将此格式化字符串写入文件时,我只得到类似的内容"java.io.PrintStream@84fc8d"
in it.
查看文档后了解到这个方法有点像System.out.print()
并返回一个 PrintStream 来显示(例如在控制台中),所以我尝试使用.toString
or String.valueOf()
但我得到同样的结果。
所以我想知道是否有一种方法可以像这样格式化字符串String.out.format()
方法确实如此,但以可写入文件的方式?
这是我使用的大约代码(只是让有用的部分出现)
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();
}
System.out.format
回报PrintStream
Object
and toString
方法调用给你java.io.PrintStream@84fc8d
你正在尝试写的内容。
你应该使用String.format反而。
i.e
WRITE_MY_LINE(String.format(" I = %3d var = %9.6f", i, myVar));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)