按日期和时间将巨大文件拆分为多个文件

时间:2014-05-29 10:13:13

标签: java

我按日期和时间(每个文件12小时)将一个巨大的文件(2GB)拆分成多个文件。但是在分割成多个文件时,它只创建了一些文件。

20131101053000.txt
20131101173001.txt
20131102053002.txt
20131102173003.txt
20131103053004.txt
after that some more files to be created, but not creating.

拆分文件(已更新):

splitByDatenTime(timeFrameMillis, fstream); --> exception at line 30 in main method

private static void splitByDatenTime(long millis, DataInputStream fstream) {

    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    FileWriter fstream1 = null;
    BufferedWriter out = null;
    String strLine = null;
    Date prevDate = null;
    Date currDate = null;
    String dateFormat = "yyyy-MM-dd:HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    try {
        while ((strLine = br.readLine()) != null) {
            Pattern pattern = Pattern.compile("\\d+;.+");
            Matcher matcher = pattern.matcher(strLine);
            if (matcher.find()) {
                String dateString = strLine.substring(0, 10);
                Calendar cal = Calendar.getInstance();

                cal.setTimeInMillis(Long.valueOf(dateString) * 1000); --> exception at line 54

                currDate = cal.getTime();
                String currentDate = sdf.format(currDate);
                currDate = sdf.parse(currentDate);
                if (((prevDate == null) && (prevDate = currDate) != null)
                        || (prevDate.getTime() + millis < currDate
                                .getTime())) {

                    currentDate = currentDate.replaceAll("-|:", "");

                    fstream1 = new FileWriter(
                            "D:/Splited AIS Data3/"
                                    + currentDate + ".txt");
                    close(out);
                    out = new BufferedWriter(fstream1);
                    prevDate = currDate;
                }
                out.write(strLine);
                out.newLine();
            }
        }
        fstream1.close();
        close(out);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

获取NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "!ABVDM,2,2"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:430)
    at java.lang.Long.parseLong(Long.java:483)
    at com.imu.examples.SplitFile1.splitByDatenTime(SplitFile1.java:54)
    at com.imu.examples.SplitFile1.main(SplitFile1.java:30)

我无法找到的问题在哪里。请帮我。 在Adance中感谢

1 个答案:

答案 0 :(得分:1)

您需要在覆盖out之前关闭每个文件,以确保其数据完全写入。按照当前编码,只有最后一个被关闭。