kao,既不看分类,也不看别人说什么,乱弹琴!
自己写一个FormatWriter类很容易实现的:
import java.io.*;
public class FormatWriter extends PrintWriter{
private int width=10;
//固定格式宽度
public FormatWriter(Writer output){
super(output);
}
public FormatWriter(Writer output,int width){
super(output);
this.width=width;
}
public FormatWriter(Writer output,boolean autoflush){
super(output,autoflush);
}
public FormatWriter(Writer output,boolean autoflush,int width){
super(output,autoflush);
this.width=width;
}
/**
* 格式化对齐输出字符
* output fix format string
* @param s
*/
private void output(String s){
int blanks=width-s.length();
for(int i=0;i<blanks;i++)
super.print('0');
super.print(s);
}
//overload SuperClass Method
// 重载基类的Print方法,使用调用FormatWriter的output格式化输出
public void print(long value){
output(String.valueOf(value));
//以字符串表示long
}
public void print(double value){
output(String.valueOf(value)) ;//以字符串表示double
}
public void print(int value){
output(String.valueOf(value)) ;
//以字符串表示integer
}
//重载基类的println方方法,使格式化输出字符串
public void println(long value){
output(String.valueOf(value));
super.println();
//调用基类的换行输出
}
public void println(double value){
output(String.valueOf(value));
super.println();
}
public void println(int value){
output(String.valueOf(value));
super.println();
}
public void setWidth(int width){
this.width=width>0?width:1;
}
public int getWidth(){
return width;
}
}