取文件分为多个文件
程序员文章站
2022-05-29 20:22:29
...
手动查库生成的文件放在一个txt文件里了,上传ftp要求十万一个txt,就临时写了个工具
package com.ailk.ecs.ssp.test; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; public class orderDate { public static final int X = 103; @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) throws Exception { File file = new File("C:\\Desktop\\123\\20180322PLUS_04.TXT"); LineIterator it = FileUtils.lineIterator(file, "UTF-8"); ArrayList list1 = new ArrayList(); int filelenth = (int) file.length(); int i = 1; try { while (it.hasNext()) { String line = it.nextLine(); list1.add(line + "\r\n"); if (list1.size() % 100000 == 0) { getfile(list1, i); list1.clear(); i++; } } getfile(list1, i); } finally { LineIterator.closeQuietly(it); } } private static void getfile(ArrayList<String> list1, int i) throws IOException { BufferedWriter bw = null; if (list1.size() == 0) { } else { try { System.out.println(list1.size()); String url = "C:\\Desktop\\321\\"; i = i + X; File file1 = new File(url + (i > 9 ? "20180328PLUS_" + i : "20180328PLUS_0" + i) + ".txt");// (i < 10 ? Name + "_0" + i : Name + "_" + i) FileOutputStream out = new FileOutputStream(file1); OutputStreamWriter outwriter = new OutputStreamWriter(out, "UTF-8"); bw = new BufferedWriter(outwriter); bw.write(list1.size() + 2 + "\r\n"); for (String line : list1) { bw.write(line); } } finally { bw.flush(); bw.close(); } } } }
上一篇: XML使用DOM4J方式读写