0

我想用 Format 类右对齐这个简单的文本文件(行长为 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     

我究竟做错了什么?

4

0 回答 0