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

我正在尝试将文件从一个文件拆分为 4 个不同的文件。所以我将文件除以一些“x”值,并希望将文件写入该值,然后从那里到下一个文件继续,直到文件内容结束。


我正在使用缓冲区读取器检查文件中的一些 x 值,并检查内容是否等于 x 值并进行拆分。


拆分即将到来,但以另一种方式,就像它正在读取文件并写入到“x”的行号。但是我需要所有行,直到文件中存在“x”值。


我在文件中有一个时间,比如开始时间 hh:mm:ss,我正在用 hh:mm:ss 和我的 x 值检查这个,并进行如下拆分


// 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(":");


我正在使用一些数字增加时间以读取文件中的下一行并读取到另一个文件中。


这里 splitlen 是 30 ,因此它将数据写入新文件中的 30 行。然后它增加 splitlen+30 即 60。但是,它正在读取接下来的 60 行并写入下一个文件。


但是我需要使用文件内容中提供的时间检查此 splitlen,我应该拆分该行。


请建议我哪里做错了。如果您提供片段,将不胜感激。


蝴蝶不菲
浏览 110回答 2
2回答

守候你守候我

您的代码的问题在于您正在查看的时间戳在 HH:MM:ss 中,但使用splitlen和x变量您只能使用分钟。所以你需要跟踪小时和分钟,也许这可以用一些 DateTime 类来完成,但这里有一个简单的 int 解决方案//somewhere at the top&nbsp;int hour = 0;int minutes = 30;//where you today increase splitlenminutes += 30;if (minutes == 60) {&nbsp; &nbsp;hour++;&nbsp; &nbsp;minutes = 0;}//parse also hoursint y = Integer.parseInt(arrOfStr[0]);int x = Integer.parseInt(arrOfStr[1]);//you need to rewrite this to compare x and y against hour and minuteswhile (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) {&nbsp; &nbsp; int count = 1;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; File filename = new File(filepath);&nbsp; &nbsp; &nbsp; &nbsp; InputStream infile = new BufferedInputStream(new FileInputStream(filename));&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(new InputStreamReader(infile));&nbsp; &nbsp; &nbsp; &nbsp; FileWriter outfile = createOutputFile(count);&nbsp; &nbsp; &nbsp; &nbsp; boolean isEndOfFile = false;&nbsp; &nbsp; &nbsp; &nbsp; while (!isEndOfFile) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outfile.write(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.trim().isEmpty()) { //last line of group&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == 2) { //Timestamp row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] split = line.split("-->");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checkTime(split)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outfile.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outfile.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outfile = createOutputFile(count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private FileWriter createOutputFile(int index) {&nbsp; &nbsp; //Create new outputfile and writer&nbsp;&nbsp; &nbsp; return null;&nbsp;}private boolean checkTime(String[] arr) {&nbsp; &nbsp; //use start or end time in arr to check if an even half or full hour has been passed&nbsp; &nbsp; return true;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java