如何在java txt文件中打印列 [英] how to print columns in java txt file
本文介绍了如何在java txt文件中打印列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在一个.txt文件上打印多个数组
我不记得格式化命令做这个
我需要所有列对齐现在我有这个
private static void makeFile(String [] name,String [] nickname,String [] capital,
String [] flowers,String [] population)throws FileNotFoundException
{
PrintWriter out =新的PrintWriter(out.txt); (int i = 0; i <50; i ++)
out.println(name [i] +\ t+昵称[i])的
;
out.close();
$ b $ p
$ b $ p
$ b $
如何我解决它,所以他们都对齐,并有3个更多的列添加到这个如何让他们对齐,以及??
解决方案您应该使用 String.format
like
字符串。格式(% - 30s%s,名称[i],昵称[i])
30是名字的最大长度。
I am trying to print multiple arrays on one .txt file having one array print then have another column crated and have another array print how do i format this to work??
I cant remember the formatting commands to do this
I need all the columns to align right now i have this
private static void makeFile(String[] name, String[] nickname, String[] capital,
String[] flowers, String[] population) throws FileNotFoundException
{
PrintWriter out = new PrintWriter ("out.txt");
for (int i = 0; i< 50 ; i++)
out.println( name[i] +" \t "+ nickname[i] );
out.close();
}
This is what prints
How do i fix it so they are all aligned and there are 3 more columns to add to this how do i get them to align as well ??
解决方案 You should use String.format
like
String.format("%-30s %s", name[i], nickname[i])
where 30 is maximum length of name.
这篇关于如何在java txt文件中打印列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文