1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.tohours.agent5v5.util;
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map;
import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.log4j.Logger;
public class Test {
private static Logger log = Logger.getLogger(Test.class);
public static void loadCsvData(String csvPath) { try {
File dataFile = new File(csvPath); byte[] lastByte = {}; Integer length = 94372 * 5; Boolean isOver = false; FileInputStream input = new FileInputStream(dataFile); Map<String, Object> rv = null; String content = ""; String charset = "GBK"; while (!isOver) {
if (rv != null && rv.containsKey("lastByte")) { lastByte = (byte[]) rv.get("lastByte"); } rv = loadContent(length, input, lastByte, charset); isOver = (Boolean) rv.get("isOver"); content = (String) rv.get("content"); log.info("content:" + content); } } catch (Exception e) { e.printStackTrace(); } }
private static Map<String, Object> loadContent(Integer length, InputStream input, byte[] la, String charset) throws IOException { Map<String, Object> rv = new HashMap<String, Object>(); byte[] buffer = new byte[la.length + length]; copyToBuffer(buffer, la); Integer read = IOUtils.read(input, buffer, la.length, length); byte hh = 10; int lastN = ArrayUtils.lastIndexOf(buffer, hh); byte[] conArray = ArrayUtils.subarray(buffer, 0, lastN); byte[] lastByte = ArrayUtils.subarray(buffer, lastN, buffer.length); String content = new String(conArray, 0, conArray.length, charset);
rv.put("isOver", read < length); rv.put("content", content); rv.put("lastByte", lastByte); return rv; }
private static void copyToBuffer(byte[] buffer, byte[] la) { for (int i = 0; i < la.length; i++) { buffer[i] = la[i]; } }
public static void main(String[] args) { String csvPath = "C:\\20180630115435.csv"; loadCsvData(csvPath); }
}
|