Java:使用Format类的右对齐不起作用

时间:2013-07-09 18:29:14

标签: java text-justify

我想用Format class:

对这个简单的文本文件(行长度为80)进行右对齐

每个空格都是[TAB]

|Text|EOL                                                                   
|   Text    Text|EOL                                                        
|       Text    Text    Text|EOL                                            
|           Text    Text    Text    Text|EOL                                
|               Text    Text    Text    Text    Text|EOL                    
|                   Text    Text    Text    Text    Text    Text|EOL        
|                       Text    Text    Text    Text    Text    Text|EOL    
|                           Text    Text    Text    Text    Text    Text|EOL                            

这是我写的代码:

public static void justify(){
    try(
            BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Szymon\\Desktop\\textfile.txt"));
            PrintWriter out = new PrintWriter("C:\\Users\\Szymon\\Desktop\\textout.txt")
    ) {
        while(in.ready()){
            String line = in.readLine();

            out.format("%80s\n", line.trim()); // ?
        }
    } catch (Exception e){
        e.printStackTrace();
    }
}

但是输出并不是我所期望的:

|                                                                            Text|EOL
|                                                                      Text Text|EOL
|                                                                  Text Text    Text|EOL
|                                                             Text  Text    Text    Text|EOL
|                                                        Text   Text    Text    Text    Text|EOL
|                                                   Text    Text    Text    Text    Text    Text|EOL
|                                                   Text    Text    Text    Text    Text    Text|EOL
|                                                   Text    Text    Text    Text    Text    Text|EOL     

我做错了什么?

0 个答案:

没有答案