English 中文(简体)
如何在特定的格式显示数据从txt文件吗
原标题:
  • 时间:2009-04-08 21:24:19
  •  标签:

我有一个文本文件输入包含数据如下。我怎么能显示文本文件到特定格式的数据?

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
最佳回答

我只是把这个一起很快,但类似:

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	Name	Type of Exercise	Time
");
    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( 	 );
        str.append(activity.name);
        str.append( 	 );
        str.append(activity.typeOfExercise);
        str.append( 	 );
        str.append(activity.time);
        str.append( 
 );
    }
    System.out.print(str.toString());
}

然后调用所有的例如:

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

我会看看Java s < a href = " http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html printf 28以%,% 20 Java . lang . object…% 29”rel = " nofollow noreferrer " > < / > sprintf()函数和它年代能力< a href = " http://www.java2s.com/Tutorial/Java/0120__Development/Rightjustifyingandleftjustifyingvalues.htm " rel = " nofollow noreferrer " >左/右证明< / >数据与指定的宽度。

<强>关于解析输入:< /强>

One issue you will have is that each "record" of data (each row, in the ouput) is not a fixed size. Some are 3-tuples of name,exercise,time, and others are 4-tuples of day,name,exercise,time

说,假设你已经给定的格式是真的都有,这个问题可以在工作。

After reading a line, you could check for a weekday, and if so assume that s the start of a 4-tuple, and read the next 3 lines. If it is not a weekday, then assume it is a 3-tuple, and only read the next 2 lines.

如果可能有“差距”的名称,类型,或时间列在输出中,在不同的组合,它将变得更加棘手。

你真的需要您的程序有特殊知识什么列中的值是有效的。如杰西卡不是一种有效的锻炼,慢跑并不是一个有效的名字。

<强>关于格式化输出< /强>

731952年< a href = " https://stackoverflow.com/questions/731917/how-to-display-data-from-txt-file-in-specific-format/731952 " >布莱恩回答< / >是相关的。

It depends on the language you use. Most languages have a printf-equivalent. The formatting codes of printf allow you to pad with space, etc.

如果您正在使用Perl(可能适合这个任务),您可以使用< a href = " http://www.perl.com/doc/manual/html/pod/perlform.html " rel = " nofollow noreferrer " > < / >格式





相关问题
热门标签