如何以特定格式显示txt文件中的数据

时间:2009-04-08 21:24:20

标签: java file text-files

我有一个文本文件输入,其中包含如下数据。如何将文本文件中的数据显示为特定格式?

Monday
Jessy
Walking
20 minutes
Matthew
Run
20 minutes
Karen
Jogging
40 minutes
Jessica
Run
12 minutes
Tuesday
Messia
Walking
10 minutes
Matthew
Run
20 minutes
Pete
Run
10 minutes
Carol
Walking
30 minutes

我想将文本文件中的数据显示为以下格式:

Day            Name               Type of exercise           Time
Monday         Jessy              Walking                    20 minutes
               Matthew            Run                        20 minutes
               Karen              Jogging                    40 minutes
               Jessica            Run                        12 minutes 
Tuesday        Messia             Walking                    10 minutes
               Matthew            Run                        20 minutes
               Pete               Run                        10 minutes
               Carol              Walking                    30 minutes

3 个答案:

答案 0 :(得分:4)

我只是把它快速地扔在了一起,但是像这样的东西:

static final String[] DAYS =
{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

public class ActivityEvent
{
  public int day;
  public String name;
  public String typeOfExercise;
  public String time;
}

public List loadActivities(String filename) throws IOException
{
  List activities = new ArrayList();
  FileInputStream fis = new FileInputStream(filename);
  InputStreamReader isr = new InputStreamReader(fis);
  BufferedReader br = new BufferedReader(isr);
  int lastDay = -1;
  String line;
  while ((line = br.readLine()) != null)
  {
      line = line.trim();
      int day;
      for (day = DAYS.length - 1; day >= 0; day--)
      {
          if (line.equals(DAYS[day]))
          {
              break;
          }
      }
      String name;
      if (day < 0)
      {
          day = lastDay;
          if (lastDay < 0)
          {
              throw new IOException(filename + " must start with day of week");
          }
          name = line;
      }
      else
      {
          name = br.readLine();
          if (name == null)
          {
              throw new IOException(filename + " expected name, reached end of file");
          }
      }
      String type = br.readLine();
      if (type == null)
      {
          throw new IOException(filename + " expected type of exercise, reached end of file");
      }
      String time = br.readLine();
      if (time != null)
      {
          throw new IOException(filename + " expected time of exercise, reached end of file");
      }
      ActivityEvent activity = new ActivityEvent();
      activity.day = day;
      activity.name = name;
      activity.typeOfExercise = type;
      activity.time = time;
      activities.add(activity);
  }
  return activities;
}

public void printActivities(List activities)
{
    StringBuilder str = new StringBuilder("Day\tName\tType of Exercise\tTime\n");
    int numActivities = activities.size();
    int lastDay = -1;
    for (int index = 0; index < numActivities; index++)
    {
        ActivityEvent activity = (ActivityEvent)activities.get(index);
        if (activity.day != lastDay)
        {
            str.append(DAYS[activity.day]);
        }
        str.append('\t');
        str.append(activity.name);
        str.append('\t');
        str.append(activity.typeOfExercise);
        str.append('\t');
        str.append(activity.time);
        str.append('\n');
    }
    System.out.print(str.toString());
}

然后调用所有内容,例如:

List activities = loadActivities("somefile.txt");
// Do optional sorting, etc. here.
printActivities(activities);

答案 1 :(得分:0)

我会看看Java的sprintf()函数,它能够left/right justify具有指定宽度的数据。

答案 2 :(得分:0)

关于解析输入:

您将遇到的一个问题是数据的每个“记录”(每行,在输出中)都不是固定大小。 有些是3元组的名字,运动,时间,其他是4元组的一天,名字,运动,时间

那就是说,假设你给出的格式真的是全部,那么这个问题就可以解决了。

读完一行之后,你可以检查一个工作日,如果是这样的话,假设这是4元组的开始,并读取接下来的3行。 如果它不是工作日,那么假设它是一个3元组,并且只读取接下来的2行。

如果输出中的名称,类型或时间列中可能存在“间隙”,并且在不同的组合中,它会变得更加棘手。

您确实需要您的程序具有关于哪些值在哪些列中有效的特殊知识。例如,'Jessica'不是一种有效的运动类型,'慢跑'不是有效名称。

关于格式化输出

Brian's answer是相关的。

这取决于您使用的语言。大多数语言都有printf等价物。 printf的格式代码允许您填充空格等

如果您使用的是Perl(可能非常适合此任务),您可以使用formats