我有一个包含此内容的文本文件
08:00 - 09:30
09:45 - 11:15
11:30 - 13:00
08/04/13
14:00 - 15:30
15:45 - 17:15
17:30 - 19:00
MIS (CH27) UM
MIS (CH27) UM
每条空线对应一周中的一天,08/04/14 是一周的第一天。我想读取文本文件,计算 MIS 发生的日期并打印出来。这是我的程序,它在结果中什么也没显示。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class UsingPattern {
public static final String SOURCE = "results/timetable1.txt";
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
new UsingPattern().readFile(SOURCE);
}
public void readFile(String filename) throws ParseException {
try {
File file = new File(filename);
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
String line;
Calendar cal = new GregorianCalendar(); // Create a Calendar object
while ((line = in.readLine()) != null) { // Read lines, check for end-of-file
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy"); // Create a format for date input from text file
Date date = null;
if (line.matches("\\d{2}/\\d{2}/\\d{2}")) { // If the line has this format, put it as date
date = dateFormat.parse(line);
cal.setTime(date); // Sets this Calendar's time with the given Date.
}
else { // Each empty line correspond to 1 day
if ((in.readLine()) == " ") {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
else {
if (line.matches("MIS")) {
String fday = dateFormat.format(cal.getTime());
System.out.println((fday));
System.out.println(line);
}
}
}
}
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
有人能指出我哪里做错了吗!清你!