如何在java txt文件中打印列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20202563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:13:37  来源:igfitidea点击:

how to print columns in java txt file

javaformatting

提问by dondon4720

I am trying to print multiple arrays on one .txt file having one array print then have another column crated and have another array print how do i format this to work??

我正在尝试在一个 .txt 文件上打印多个数组,打印一个数组,然后将另一列装箱并打印另一个数组,我该如何格式化才能工作?

I cant remember the formatting commands to do this I need all the columns to align right now i have this

我不记得执行此操作的格式化命令我现在需要对齐所有列我有这个

private static void makeFile(String[] name, String[] nickname, String[] capital, 
           String[] flowers, String[] population) throws FileNotFoundException 
{
    PrintWriter out = new PrintWriter ("out.txt");

    for (int i = 0; i< 50 ; i++)
         out.println( name[i] +" \t "+  nickname[i] );

    out.close();
}

This is what prints

这是打印出来的

enter image description here

在此处输入图片说明

How do i fix it so they are all aligned and there are 3 more columns to add to this how do i get them to align as well ??

我如何修复它以便它们都对齐并且还有 3 列要添加到此我如何让它们对齐?

回答by Basim Khajwal

The problem you are having is that some names are larger than a tab so the column is getting messed up, try using two \ts instead. E.g. one tab is 8 characters so anything more will be on a different tab to anything less than 8 characters.

您遇到的问题是某些名称比选项卡大,因此列变得混乱,请尝试使用两个 \ts 代替。例如,一个制表符是 8 个字符,因此与少于 8 个字符的任何内容都将在不同的制表符上。

回答by Nikolay

You should use String.formatlike

你应该使用String.format

String.format("%-30s %s", name[i], nickname[i])

where 30 is maximum length of name.

其中 30 是名称的最大长度。

回答by 0605002

You can use printfinstead of println, with C-style format strings.

您可以使用printf,而不是println与C风格的格式字符串。

out.printf("%-16s%-24s\n", name[i], nickname[i]);

This will print the name[i]left-aligned in a 16-character placeholder, then print nickname[i]in a 24-character wide column. When adding more columns, you can specify the maximum required number of characters in the format string. The -sign is added for aligning the strings to left.

这将name[i]在 16 个字符的占位符中打印左对齐,然后nickname[i]在 24 个字符宽的列中打印。添加更多列时,您可以指定格式字符串中所需的最大字符数。该-符号添加对准字符串左侧。

回答by Elliott Frisch

You could do something like this

你可以做这样的事情

// Get the maximum length of any string in the array, or 0.
private static int getMaxLength(String[] in) {
  int c = 0;
  if (in != null && in.length > 0) {
    for (String i : in) {
      i = (i != null) ? i.trim() : "";
      if (i.length() > c) {
        c = i.length();
      }
    }
  }
  return c;
}

// Pad any input string to the minimum length.
private static String padString(String in, int min) {
  in = (in != null) ? in.trim() : "";
  StringBuilder sb = new StringBuilder(in);
  while (sb.length() < min) {
    sb.append(' ');
  }
  return sb.toString();
}

private static void makeFile(String[] name,
  String[] nickname, String[] capital,
  String[] flowers, String[] population) {
  PrintWriter out = null;
  try {
    out = new PrintWriter("out.txt");

    // Add 1 to get at least 1 space between the maximum and the next item.
    int namePadded = getMaxLength(name) + 1;
    int nickPadded = getMaxLength(nickname) + 1;
    int capitalPadded = getMaxLength(capital) + 1;
    int flowersPadded = getMaxLength(flowers) + 1;
    int populationPadded = getMaxLength(population);

    for (int i = 0; i < name.length; i++) {
      out.println(padString(name[i], namePadded)
        + padString((nickname.length > i) ? nickname[i]
                : "", nickPadded)
        + padString((capital.length > i) ? capital[i] : "",
            capitalPadded)
        + padString((flowers.length > i) ? flowers[i] : "",
            flowersPadded)
        + padString((population.length > i) ? population[i]
                : "", populationPadded));
    }
  } catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace(System.err);
  } finally {
    out.close();
  }
}

public static void main(String[] args) {
  String[] name = { "Alabama", "Alaska", "Arizona" };
  String[] nickname = { "Yellowhammer State",
    "Last Frontier", "Grand Canyon State" }; 
  String[] capital = {
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "Y", "Z" };
  String[] flowers = { "Rose", "Carnation", "Orchid" };
  String[] population = { "1", "100", "1000" };
  makeFile(name, nickname, capital, flowers,
    population);
}