Java 输出文件格式
我在格式化输出文件以使其易于阅读时遇到了一些麻烦。我正在尝试输出文件,例如:
Name of School Size of School
-------------- --------------
Blblah blah 1300
Blah blah blah 11300
Blah blah blah asdf 14220
Blah bblah 1300
但遇到麻烦。目前,我正在使用以下代码来获取以下输出:
File file1 = new File("src\\sortedInt.txt");
Formatter fmt = new Formatter(file1);
HelperMethods.quickSortStudents(colleges, 0, colleges.length - 1);
for(int i = 0; i < colleges.length; i++)
{
fmt.format(colleges[i].nameOfSchool + "%20d" + "\n", (int)colleges[i].numOfStudents);
fmt.flush();
}
这给了我:
eastman school of music 800
clark university 1100
walla walla college 1100
drew 1200
juilliard 1200
因为我只是从大学名称的末尾开始填充。是否有办法填充整个字符串,以便所有字符串的长度都是恒定的?
谢谢大家的帮助
I am having a bit of trouble formatting an output file for myself to be easily readable. I am trying to output the file such as:
Name of School Size of School
-------------- --------------
Blblah blah 1300
Blah blah blah 11300
Blah blah blah asdf 14220
Blah bblah 1300
but am having trouble. Currently I am using the following code to get the following output:
File file1 = new File("src\\sortedInt.txt");
Formatter fmt = new Formatter(file1);
HelperMethods.quickSortStudents(colleges, 0, colleges.length - 1);
for(int i = 0; i < colleges.length; i++)
{
fmt.format(colleges[i].nameOfSchool + "%20d" + "\n", (int)colleges[i].numOfStudents);
fmt.flush();
}
which gives me:
eastman school of music 800
clark university 1100
walla walla college 1100
drew 1200
juilliard 1200
Because I am just padding from the end of the college name. Is there anyway to pad the whole string so all the strings are a constant length?
Thank you everyone for you help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,输出你的大学名称左对齐并填充到一定的长度,然后输出你的学生人数右对齐并填充到一定的长度:
Yep, output your college name left-justified and padded to a certain length, then output your number of student right-justified and padded to a certain length:
您可能还想添加要格式化的学校名称,如下所示:
以下链接可以让您更深入地了解格式化:
http://download.oracle.com/javase /1.5.0/docs/api/java/util/Formatter.html
You might want to add the name of the schools to be formatted too, as the following:
The following link can give you more insight in formatting:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html