2 回答
![?](http://img1.sycdn.imooc.com/54584d560001571a02200220-100-100.jpg)
TA贡献1802条经验 获得超10个赞
您的代码的问题在于您正在查看的时间戳在 HH:MM:ss 中,但使用splitlen和x变量您只能使用分钟。
所以你需要跟踪小时和分钟,也许这可以用一些 DateTime 类来完成,但这里有一个简单的 int 解决方案
//somewhere at the top
int hour = 0;
int minutes = 30;
//where you today increase splitlen
minutes += 30;
if (minutes == 60) {
hour++;
minutes = 0;
}
//parse also hours
int y = Integer.parseInt(arrOfStr[0]);
int x = Integer.parseInt(arrOfStr[1]);
//you need to rewrite this to compare x and y against hour and minutes
while (data != -1 && x < splitlen) {
因此,现在您将不会寻找 30、60、90...分钟,而是 00:30、01:00、01:30 等。当然,您还必须准备好处理一整分钟没有人进入的情况,除非您当然已经这样做了。
checkTime当然是这里的一个关键方法,当文件被拆分为类成员时,制作最后一小时和一分钟可能是个好主意,但它们当然也可以作为参数从split().
更新
这是该split方法的简化版本,用于举例说明如何解决此问题,它并不完整,但应该是解决问题的一个很好的起点。我尝试利用.str文件的构造方式并利用上面解释的逻辑来确定何时打开新的输出文件。
public void split(String filepath, long splitlen, String name) {
int count = 1;
try {
File filename = new File(filepath);
InputStream infile = new BufferedInputStream(new FileInputStream(filename));
BufferedReader br = new BufferedReader(new InputStreamReader(infile));
FileWriter outfile = createOutputFile(count);
boolean isEndOfFile = false;
while (!isEndOfFile) {
String line = null;
int i = 1;
while ((line = br.readLine()) != null) {
outfile.write(line);
if (line.trim().isEmpty()) { //last line of group
i = 1;
continue;
}
if (i == 2) { //Timestamp row
String[] split = line.split("-->");
if (checkTime(split)) {
count++;
outfile.flush();
outfile.close();
outfile = createOutputFile(count);
}
}
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private FileWriter createOutputFile(int index) {
//Create new outputfile and writer
return null;
}
private boolean checkTime(String[] arr) {
//use start or end time in arr to check if an even half or full hour has been passed
return true;
}
添加回答
举报