0

I want to right-justify this simple text file (line length is 80) with Format class:

Each white-space is [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                            

Here is the code I wrote:

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();
    }
}

but the output is not what I've expected:

|                                                                            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     

What am I doing wrong?

ashur
  • 4,177
  • 14
  • 53
  • 85

0 Answers0