Java 使用Google Guava格式化字符串
我正在使用GoogleGuava格式化字符串,并使用BufferedWriter将其写入文本文件Java 使用Google Guava格式化字符串,java,string,guava,pad,Java,String,Guava,Pad,我正在使用GoogleGuava格式化字符串,并使用BufferedWriter将其写入文本文件 import com.google.common.base.Strings; import java.io.*; public class Test { public static void main(String[] args) { File f = new File("MyFile.txt"); String firstName = "STANLEY"
import com.google.common.base.Strings;
import java.io.*;
public class Test {
public static void main(String[] args) {
File f = new File("MyFile.txt");
String firstName = "STANLEY";
String secondName = "GATUNGO";
String thirdname = "MUNGAI";
String location = "NAIROBI";
String school = "STAREHE BOYS CENTRE";
String yob= "1970";
String marital = "MARIED";
String texture = "LIGHT";
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
if (!f.exists()) {
f.createNewFile();
}
bw.write(Strings.padStart(firstName, 0, ' '));
bw.write(Strings.padStart(secondName, 20, ' '));
bw.write(Strings.padStart(thirdname, 20, ' '));
bw.write(Strings.padStart(location, 20, ' '));
bw.newLine();
bw.write(Strings.padStart(school, 0, ' '));
bw.write(Strings.padStart(yob, 20, ' '));
bw.write(Strings.padStart(marital, 20, ' '));
bw.write(Strings.padStart(texture, 20, ' '));
bw.close();
} catch (Exception asd) {
System.out.println(asd);
}
}
}
我的输出是
我需要输出为
我从数据库和数据库接收字符串,上面的硬编码只是一个示例,我需要编写字符串,使第一个字符串的长度不会影响第二个字符串的起始位置。我需要用谷歌番石榴从同一个位置开始,以列的方式写它们。
我也会感谢你的例子。我不确定你能做到,尽管我没有使用谷歌番石榴。这里有一个建议,将文件写入CSV,一个逗号分隔的文件。为此,只需将输出更改为MyFile.csv,然后在写入的每个值后添加逗号“,”。此文件可以在任何电子表格程序中打开,每个值都在其自己的单元格中,因此解决方案将是整洁的 如果这不起作用,即,如果它必须是一个txt文件,这里是另一个想法。循环遍历您必须写入的每一个字符串,找到最长的字符串,然后通过在其中添加空格将其他字符串调整为该长度 我希望这有帮助
String.format
您可以简单地使用而不是。它遵循C printf语法。我认为它符合你的需要
String aVeryLongString="aaabbbcccdddeeefffggghh";
String aShortString="abc";
String anotherString="helloWorld";
String formattedString=String.format("%5.5s,%5.5s,%5.5s",aVeryLongString,aShortString,anotherString);
System.out.println(formattedString);
输出:
aaabb,abc,你好
正如您所见,长字符串被剪切,短字符串被填充。因为类路径上已经有番石榴了。。我将向你们展示我的想法,但这可能过于简化了,需要做更多的工作,即使不是太多 您需要始终知道一行中字符串的最大大小。我在这里只计算了一个:
private static final class SortBylenght extends Ordering<String>{
@Override
public int compare(String left, String right) {
return Ints.compare(left.length(), right.length());
}
}
private static final类SortBylenght扩展了排序{
@凌驾
公共整数比较(左字符串、右字符串){
返回int.compare(left.length(),right.length());
}
}
然后计算最大值:
ImmutableList<String> data = ImmutableList.of(firstName, school);
SortBylenght sortBylenght = new SortBylenght();
String maxString = sortBylenght.max(data);
int maxSize = maxString.length();
ImmutableList data=ImmutableList.of(名字,学校);
SortBylenght SortBylenght=新的SortBylenght();
字符串maxString=sortBylenght.max(数据);
int maxSize=maxString.length();
然后打印:
bw.write(Strings.padEnd(firstName, maxSize, ' '));
for(int i=0;i<20;++i) bw.write(' ');
//You need to pad the end here of course again like in the first row;
bw.write(secondName);
bw.newLine();
bw.write(Strings.padEnd(school, maxSize, ' '));
for(int i=0;i<20;++i) bw.write(' ');
//You need to pad the end here of course again like in the first row;
bw.write(yob);
bw.write(Strings.padEnd(firstName,maxSize');
对于(int i=0;i使用padEnd()
,而不是padStart()
padStart()
,如果您想右对齐列的话。感谢您通过bw.write(String.format(%-20.20s%-20.20s%-20.20s%-20.20s%),firstName,secondName,thirdName,location)的方式来完成
您的课程理念这里的要点是使用padEnd()
,而不是padStart()
,作为固定列格式。