【发布时间】:2018-07-29 16:40:22
【问题描述】:
我正在尝试将文件从一个文件拆分为 4 个不同的文件。所以我将文件除以某个“x”值,并想将文件写入该值,然后从那里继续到下一个文件,直到文件内容结束。
我正在使用缓冲区阅读器检查文件中的一些 x 值,并检查内容是否等于 x 值并进行拆分。
拆分即将到来,但以另一种方式,比如它正在读取文件并写入到行号为“x”。但我需要所有的行,直到文件中出现“x”值。
我在文件中有一个时间,比如开始时间 hh:mm:ss,我正在用我的 x 值检查 hh:mm:ss 并进行如下拆分
// inputs to the below method
// filePath = "//somepath";
// splitlen = 30;
// name ="somename"; */
public void split(String FilePath, long splitlen, String name) {
long leninfile = 0, leng = 0;
int count = 1, data;
try {
File filename = new File(FilePath);
InputStream infile = new BufferedInputStream(new FileInputStream(filename));
data = infile.read();
BufferedReader br = new BufferedReader(new InputStreamReader(infile));
while (data != -1) {
filename = new File("/Users//Documents/mysrt/" + count + ".srt");
OutputStream outfile = new BufferedOutputStream(new FileOutputStream(filename));
String strLine = br.readLine();
String[] atoms = strLine.split(" --> ");
if (atoms.length == 1) {
// outfile.write(Integer.parseInt(strLine + "\n"));
}
else {
String startTS = atoms[0];
String endTS = atoms[1];
System.out.println(startTS + "\n");
System.out.println(endTS + "\n");
String startTime = startTS.replace(",", ".");
String endTime = endTS.replace(",", ".");
System.out.println("startTime" + "\n" + startTime);
System.out.println("endTime" + "\n" + endTime);
String [] arrOfStr = endTime.split(":");
System.out.println("=====arrOfStr=====");
int x = Integer.parseInt(arrOfStr[1]);
System.out.println(arrOfStr[1]);
System.out.println("===x repeat==");
System.out.println(x);
System.out.println("===splitlen repeat==");
System.out.println(splitlen);
System.out.println(data);
System.out.println(br.readLine());
System.out.println(br.read());
while (data != -1 && x < splitlen) {
outfile.write(br.readLine().getBytes());
data = infile.read();
x++;
}
System.out.println("===== out of while x =====");
System.out.println(br.readLine());
System.out.println(x);
leninfile += leng;
leng = 0;
outfile.close();
firstPage = false;
firstPage = true;
count++;
splitlen = splitlen + 30;
System.out.println("=====splitlen after=====" +splitlen);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
我正在用一些数字增加时间,以读取文件中的下一行并读取到另一个文件中。
这里的 splitlen 是 30 ,所以它将数据写入新文件中的 30 行。然后它增加 splitlen+30 即 60。但是,它正在读取接下来的 60 行并写入下一个文件。
但是我需要用文件内容中提供的时间检查这个 splitlen,我应该拆分那行。
请建议我哪里做错了。如果您提供 sn-p 将不胜感激。
谢谢。
【问题讨论】:
-
我不太明白你的问题,但我想指出,每次你做
System.out.println(br.readLine());时,你都是从文件中读取一行而不处理读取的行。 -
嗨 @JoakimDanielson 我希望你知道 .srt 字幕文件是怎样的。我的实际需要是读取文件的每一行,然后用行中的 endTime 检查 x 值。因此,如果我的 x 值为 30,我将逐行检查所有行,并将停止读取匹配的位置并写入新文件,直到该行满足我的 x 值但在上面的代码中写入新文件,直到文件中的 30 行不是我要找的
-
好的,但正如我在之前的评论中所写,您正在阅读的文件中跳过了一些行。
标签: java inputstream filereader outputstream