Ver código fonte

文件分割

xufei 2 anos atrás
pai
commit
d6f0b12285
1 arquivos alterados com 68 adições e 0 exclusões
  1. 68 0
      src/test/java/com/winhc/task/FileSplit.java

+ 68 - 0
src/test/java/com/winhc/task/FileSplit.java

@@ -0,0 +1,68 @@
+package com.winhc.task;
+
+import java.io.*;
+
+/**
+ * @author π
+ * @Description:
+ * @date 2022/11/4 10:44
+ */
+public class FileSplit {
+    public static void main(String[] args) {
+        try {
+            String path = "D:\\Soft\\odpscmd_public\\bin\\out_export_company_data_shanxi_data_final.csv";
+            String prefixPath = "D:\\data\\split\\";
+            File file = new File(path);
+            InputStreamReader r = new InputStreamReader(new FileInputStream(file));
+            BufferedReader reader = new BufferedReader(r);
+            String content;
+            StringBuffer sb = new StringBuffer();
+            String lastA = "";
+            int i = 0;
+            do {
+                content = reader.readLine();
+                if (content == null) {
+                    //最后一次输出
+                    File outFile = new File(prefixPath + lastA + ".csv");
+                    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile, true));
+                    writer.write(sb.toString());
+                    writer.close();
+                    System.out.println(i);
+                    break;
+                }
+                //注:你的截取规则
+                String[] arr = content.split(",");
+                String a = arr[0];
+                content = content.substring(content.indexOf(",") + 1);
+
+                if (a.equals(lastA)) {
+                    sb.append(content).append("\n");
+                } else {
+                    if (com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils.isEmpty(a) || !a.contains("山西")) {
+                        continue;
+                    }
+                    //lastA = a;
+                    //标识改变,输出文件
+                    if(com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils.isNotBlank(lastA)){
+                        File outFile = new File(prefixPath + lastA + ".csv");
+                        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile, true));
+                        writer.write(sb.toString());
+                        writer.close();
+                    }
+                    //清空输出内容
+                    sb = new StringBuffer();
+                    sb.append(content).append("\n");
+                }
+                lastA = a;
+                i++;
+                if (i % 10000 == 0) {
+                    System.out.println(i);
+                }
+            } while (true);
+            r.close();
+            reader.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}