Writing data to text file in table format

后端 未结 4 1620
感动是毒
感动是毒 2020-12-03 19:30

So far I have this:

File dir = new File(\"C:\\\\Users\\\\User\\\\Desktop\\\\dir\\\\dir1\\\\dir2);
dir.mkdirs();
File file = new File(dir, \"filename.txt\");
         


        
4条回答
  •  自闭症患者
    2020-12-03 20:24

    The \r\n is been evaluated as part of the second parameter, so it basically calculating the required space as something like... 20 - "column 2".length() - " \r\n".length(), but since the second line doesn't have this, it takes less space and looks misaligned...

    Try adding the \r\n as part of the base format instead, for example...

    String.format("%20s %20s \r\n", "column 1", "column 2")
    

    This generates something like...

            column 1             column 2
              data 1               data 2
    

    In my tests...

提交回复
热议问题