如何以特定格式显示txt文件中的数据 [英] How to display data from txt file in specific format

查看:73
本文介绍了如何以特定格式显示txt文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text file input which contains data as below. How can I display data from the text file into specific format?

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

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

I want to display data from the text file into this format:

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

推荐答案

我很快就把它们放在一起了,但是这样的事情呢?

I just threw this together quickly, but what about something like:

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

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

And then invoke everything for example:

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

这篇关于如何以特定格式显示txt文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆