在分割成多个文件时中断了行

时间:2018-07-05 13:17:41

标签: java file split chunks srt

我将文件分成几部分,但是我面临的唯一问题是

我有.srt文件,但是在执行大块时,它会剪切字符,即在第一个.srt文件中,就像00:26:20,230->。在下一个文件中,它将继续下一个时间戳00:27:40,343。

我需要检查时间戳记是否完整,然后再检查下一个完整的字幕句子。也就是说,如果要剪切字幕时间戳或文件中的对话,则该tect将追加到下一个文件中。请建议我该如何实现。

我正在尝试如下,

    String FilePath = "/Users/meh/Desktop/escapeplan.srt";
        FileInputStream fin = new FileInputStream(FilePath);

        System.out.println("size: " +fin.getChannel().size());
        long abc = 0l;
        abc = (fin.getChannel().size())/3;
        System.out.println("6: " +abc);
System.out.println("abc: " +abc);
       //FilePath = args[1];
        File filename = new File(FilePath);
        long splitFileSize = 0,bytefileSize=0;
        if (filename.exists()) {
            try {
                //bytefileSize = Long.parseLong(args[2]);
                splitFileSize = abc;
                Splitme spObj = new Splitme();
                spObj.split(FilePath, (long) splitFileSize);
                spObj = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("File Not Found....");
        }

    public void split(String FilePath, long splitlen) {

            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();
                System.out.println("data");
                System.out.println(data);

                while (data != -1) {
                    filename = new File("/Users/meh/Documents/srt" + count + ".srt");
    //RandomAccessFile outfile = new RandomAccessFile(filename, "rw");

                    OutputStream outfile = new BufferedOutputStream(new FileOutputStream(filename));
                    while (data != -1 && leng < splitlen) {
                        outfile.write(data);
                        leng++;
                        data = infile.read();
                    }
                    leninfile += leng;
                    leng = 0;
                    outfile.close();
                    changeTimeStamp(filename, count);

                    count++;


                }
            } catch (Exception e) {
                e.printStackTrace();
            }
}

我正在尝试检查时间戳格式是否正确。然后我需要检查下一行是一个对话,然后下一行是空行。然后它可以停止块,否则应该将上一个块的文本追加到行首的下一个块文件中。使其格式正确。

我尝试检查格式,

while ((strLine = br.readLine()) != null) {

                String[] atoms = strLine.split(" --> ");
                if (atoms.length == 1) {
                    out.write(strLine + "\n");

                } else {

                    String startTS = atoms[0];
                    String endTS = atoms[1];
                    System.out.print("sri atmos start" + startTS);
                    System.out.print("sri atmos end" + endTS);
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss,SSS");

                    sdf.setLenient(false);
                    try
                    {
                        sdf.parse(startTS);
                        sdf.parse(endTS);
                        System.out.println("Valid time");
                        System.out.println("File path" + srcFileNm);
}
                    catch(Exception e) {
                        System.out.println("Invalid time");
                        System.out.println("Exception start" + startTS);
                        System.out.println("Exception end" + endTS);
}
}

我的输出块的一些屏幕, enter image description here enter image description here

帮助我如何实现这一目标。

1 个答案:

答案 0 :(得分:0)

我认为您应该更改方法,并充分使用基本的I / O方法。我试图将逻辑封装在一个小类中,该类产生具有id,msecs和字幕列表的三元组(如果我没记错的话,您可以有多行)。然后我将其余的留给了外部。 Chunker是一个类,它从文件中读取一个三元组(Class Three),以便您可以对其进行管理并将其写入某个位置。 您可以细化这只是一个“快速而肮脏”的想法,但它应该可以工作。

package org.norsam.stackoverflow;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Chunker
{

    BufferedReader r;
    int chunk = 0;
    File dir;

    public Chunker(File dir, String filename) throws IOException
    {
        File f = new File(dir, filename);
        this.dir = dir;
        this.r = new BufferedReader(new FileReader(f));
    }

    public Three readThree() throws IOException
    {
        Integer id = Integer.parseInt(r.readLine());
        String msecs = r.readLine();
        String s = null;
        List<String> srt = new ArrayList<>();
        while (!(s = r.readLine().trim()).isEmpty()) {
            srt.add(s);
        }
        return new Three(id, msecs, srt);
    }

    class Three
    {
        Integer id;
        String msecs;
        List<String> srts;

        Three(Integer id, String msecs, List<String> srts)
        {
            this.id = id;
            this.msecs = msecs;
            this.srts = srts;
        }

        Three doSomething() {
            // here you can do something with your data,
            // e.g. split msecs on "-->" and check times
            return this;
        }
        void write(BufferedWriter r) throws IOException
        {
            r.write(id);
            r.newLine();
            r.write(msecs);
            r.newLine();
            for (String s : srts) {
                r.write(s);
                r.newLine();
            }
            r.newLine();
        }
    }

    public static void main(String[] args) throws IOException
    {
        String baseDir = "/dir/where/resides/srt";
        String filename = "filename.srt";
        int elemPerChunk = 50;
        int fileNum = 0;
        File dir = new File(baseDir);
        Chunker chunker = new Chunker(dir, filename);
        boolean completed = false;
        while (!completed) {
            int srtCount = 0;
            File f = new File(baseDir, "ch." + (fileNum++) + "." + filename);
            BufferedWriter w = new BufferedWriter(new FileWriter(f));
            try {
                while (srtCount++ < elemPerChunk) {
                    chunker.readThree().doSomething().write(w);
                }
            } catch (NullPointerException e) {
                completed = true;
            }
            w.close();
        }
    }
}