Writing data to text file in table format

随声附和 提交于 2019-12-17 13:45:12

问题


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");
FileWriter archivo = new FileWriter(file);
archivo.write(String.format("%20s %20s", "column 1", "column 2 \r\n"));
archivo.write(String.format("%20s %20s", "data 1", "data 2"));
archivo.flush();
archivo.close();

However. the file output looks like this:

Which I do not like at all.

How can I make a better table format for the output of a text file?

Would appreciate any assistance.

Thanks in advance!

EDIT: Fixed!

Also, instead of looking like

    column 1             column 2
      data 1               data 2

How can I make it to look like this:

column 1             column 2
data 1               data 2

Would prefer it that way.


回答1:


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...




回答2:


I think you are trying to get data in tabular format. I've developed a Java library that can build much complex tables with more customization. You can get the source code here. Following are some of the basic table-views that my library can create. Hope this is useful enough!

COLUMN WISE GRID(DEFAULT)

+--------------+--------------+--------------+--------------+-------------+
|NAME          |GENDER        |MARRIED       |           AGE|    SALARY($)|
+--------------+--------------+--------------+--------------+-------------+
|Eddy          |Male          |No            |            23|      1200.27|
|Libby         |Male          |No            |            17|       800.50|
|Rea           |Female        |No            |            30|     10000.00|
|Deandre       |Female        |No            |            19|     18000.50|
|Alice         |Male          |Yes           |            29|       580.40|
|Alyse         |Female        |No            |            26|      7000.89|
|Venessa       |Female        |No            |            22|    100700.50|
+--------------+--------------+--------------+--------------+-------------+

FULL GRID

+------------------------+-------------+------+-------------+-------------+
|NAME                    |GENDER       |MARRIE|          AGE|    SALARY($)|
+------------------------+-------------+------+-------------+-------------+
|Eddy                    |Male         |No    |           23|      1200.27|
+------------------------+-------------+------+-------------+-------------+
|Libby                   |Male         |No    |           17|       800.50|
+------------------------+-------------+------+-------------+-------------+
|Rea                     |Female       |No    |           30|     10000.00|
+------------------------+-------------+------+-------------+-------------+
|Deandre                 |Female       |No    |           19|     18000.50|
+------------------------+-------------+------+-------------+-------------+
|Alice                   |Male         |Yes   |           29|       580.40|
+------------------------+-------------+------+-------------+-------------+
|Alyse                   |Female       |No    |           26|      7000.89|
+------------------------+-------------+------+-------------+-------------+
|Venessa                 |Female       |No    |           22|    100700.50|
+------------------------+-------------+------+-------------+-------------+

NO GRID

NAME                    GENDER       MARRIE          AGE    SALARY($)      
Alice                   Male         Yes              29       580.40      
Alyse                   Female       No               26      7000.89      
Eddy                    Male         No               23      1200.27      
Rea                     Female       No               30     10000.00      
Deandre                 Female       No               19     18000.50      
Venessa                 Female       No               22    100700.50      
Libby                   Male         No               17       800.50      
Eddy                    Male         No               23      1200.27      
Libby                   Male         No               17       800.50      
Rea                     Female       No               30     10000.00      
Deandre                 Female       No               19     18000.50      
Alice                   Male         Yes              29       580.40      
Alyse                   Female       No               26      7000.89      
Venessa                 Female       No               22    100700.50      



回答3:


You're currently including " \r\n" within your right-aligned second argument. I suspect you don't want the space at all, and you don't want the \r\n to be part of the count of 20 characters.

To left-align instead of right-aligning, use the - flag, i.e. %-20s instead of %20s. See the documentation for Formatter documentation for more information.

Additionally, you can make the code work in a more cross-platform way using %n to represent the current platform's line terminator (unless you specifically want a Windows file.

I'd recommend the use of Files.newBufferedWriter as well, as that allows you to specify the character encoding (and will use UTF-8 otherwise, which is better than using the platform default)... and use a try-with-resources statement to close the writer even in the face of an exception:

try (Writer writer = Files.newBufferedWriter(file.toPath())) {
  writer.write(String.format("%-20s %-20s%n", "column 1", "column 2"));
  writer.write(String.format("%-20s %-20s%n", "data 1", "data 2")); 
}



回答4:


try {
    PrintWriter outputStream = new PrintWriter("myObjects.txt");
    outputStream.println(String.format("%-20s %-20s %-20s", "Name", "Age", "Gender"));
    outputStream.println(String.format("%-20s %-20s %-20s", "John", "30", "Male"));
    outputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}


来源:https://stackoverflow.com/questions/26229140/writing-data-to-text-file-in-table-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!