为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

蝴蝶不菲 2021-06-11 18:01:34
我正在尝试将文件从一个文件拆分为 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,我应该拆分该行。请建议我哪里做错了。如果您提供片段,将不胜感激。
查看完整描述

2 回答

?
守候你守候我

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;

}



查看完整回答
反对 回复 2021-06-17
  • 2 回答
  • 0 关注
  • 113 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信