在某些特定行之前无法写入文件

时间:2018-07-29 16:40:22

标签: java inputstream filereader outputstream

我正在尝试将文件从一个文件拆分为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,我应该分割该行。

请向我建议我做错了什么。如果您提供摘录,将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:1)

我想这就是你想要的

public void split(String filePath, long splitLen, String name) {
    File fileSource = new File(filePath);
    int count = 0;
    boolean endOfFile = false;
    String lineSeparator = System.getProperty("line.separator");
    int hour = 0; // an accumulator for hours
    int min = 0; // an accumulator for minutes
    int sec = (int) splitLen; // an accumulator for seconds
    int _hour = 0; // hours from the file
    int _min = 0; // minutes from the file
    int _sec = 0; // seconds from the file
    try (   // try with resources to close files automatically
            FileReader frSource = new FileReader(fileSource);
            BufferedReader buffSource = new BufferedReader(frSource);
            ) {
        String strIn = null;
        while(!endOfFile) {
            File fileOut = new File("f:\\test\\mysrt\\" + count + ".srt");
            try (   // try with resources to close files automatically
                    FileWriter fwOut = new FileWriter(fileOut);
                    ) {
                if (strIn != null) {
                    // write out the last line read to the new file
                    fwOut.write(strIn + lineSeparator);
                }
                for (int i = 0; i < splitLen; i++) {
                    strIn = buffSource.readLine();
                    if (strIn == null) {
                        endOfFile = true; // stop the while loop
                        break; // exit the for loop
                    }
                    if (strIn.indexOf("-->") > 0) {
                        String endTime = strIn.split("-->")[1];
                        _hour = extractHours(endTime); // get the hours from the file
                        _min = extractMinutes(endTime); // get the minutes from the file
                        _sec = extractSeconds(endTime); // get the seconds from the file
                        if (_hour >= hour && _min >= min && _sec >= sec) { // if the file time is greater than our accumulators
                            sec += splitLen; // increment our accumulator seconds
                            if (sec >= 60) { // if accumulator seconds is greater than 59, we need to convert it to minutes and seconds
                                min += sec / 60;
                                sec = sec % 60;
                            }
                            if (min >= 60) { if accumulator minutes is greater than 59, we need to convert it to hours and minutes
                                hour += min / 60;
                                min = min % 60;
                            }
                            break; // break out of the for loop, which cause the file to be completed and a new file started.
                        }
                    }
                    fwOut.write(strIn + lineSeparator); // write out to the new file
                }
                fwOut.flush();
            }
            count++;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private int extractMinutes(String time) {
    // You need to implement this, I don't know the format of your time
    return 0;
}

private int extractSeconds(String time) {
    // You need to implement this, I don't know the format of your time
    return 0;
}

答案 1 :(得分:1)

您的代码存在问题,因为您正在查看的时间戳记是HH:MM:ss,但是使用splitlenx变量时,您只能使用分钟。

因此,您需要同时记录小时和分钟,也许可以通过某些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当然是这里的关键方法,最好是在将文件拆分为类成员时进行最后一小时和一分钟的操作,但是当然也可以将它们作为参数从{{ 1}}。

更新

这里是split()方法的简化版本,以提供有关如何解决此问题的示例,它虽然不完整,但是应该是解决此问题的良好起点。我尝试利用split文件的构造方式,并利用上述逻辑来确定何时打开新的输出文件。

.str