文章标题

来源:互联网 发布:2015年水产品出口数据 编辑:程序博客网 时间:2024/06/06 18:54
import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class Test {    static String code = "UTF-8";    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {        System.out.println("----程序开始运行----");        // 调用文件分割         read("C:\\Users\\Administrator\\Desktop\\互联网金融\\data\\tmpDir\\contract_info\\inv_contract.csv",50,'\n');    }    // 文件分割    public static int read(String path, int threadCount, char separator)            throws IOException, InterruptedException, ExecutionException {        RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r");        long fileTotalLength = randomAccessFile.length();        long[] beginIndexs = null;        long[] endIndexs = null;        long gapToEof=-1;        do {            long gap = fileTotalLength / threadCount;            long checkIndex = 0;            beginIndexs = new long[threadCount];            endIndexs = new long[threadCount];            // 分配读取位置            for (int n = 0; n < threadCount; n++) {                beginIndexs[n] = checkIndex;                if (n + 1 == threadCount) {                    endIndexs[n] = fileTotalLength;                    gapToEof=fileTotalLength;                    break;                }                checkIndex += gap;                if (checkIndex >= fileTotalLength) {                    endIndexs[n] = fileTotalLength;                    /*long[] beginc = new long[n + 1];                    long[] endInc = new long[n + 1];                    System.arraycopy(beginIndexs, 0, beginc, 0, n + 1);                    System.arraycopy(endIndexs, 0, endInc, 0, n + 1);*/                    threadCount = n + 1;                    break;                }                gapToEof = getGapToEof(checkIndex,gap, randomAccessFile, separator);                if (gapToEof<0) {                    threadCount=threadCount/2;                    gapToEof=-1;                    break;                }                checkIndex += gapToEof;                endIndexs[n] = checkIndex;            }        } while (gapToEof<0);        ExecutorService pool = Executors.newFixedThreadPool(threadCount);        // 创建多个有返回值的任务        @SuppressWarnings("rawtypes")        List<Future> list = new ArrayList<Future>();        Callable c = new MyCallable(0, beginIndexs[0], endIndexs[0], path, randomAccessFile, separator, code);        Future f2 = pool.submit(c);        for (int n = 1; n < threadCount; n++) {            long begin = beginIndexs[n];            long end = endIndexs[n];            Callable c1 = new MyCallable(n, begin, end, path, null, separator, code);            Future f = pool.submit(c1);            list.add(f);        }        // 关闭线程池        pool.shutdown();        // 获取所有并发任务的运行结果        for (Future f1 : list) {            // 从Future对象上获取任务的返回值,并输出到控制台            System.out.println(f1.get());            if (f1.get().equals("0")) {                return 0;            }        }        return 1 ;    }    private static long getGapToEof(long beginIndex, long gap, RandomAccessFile randomAccessFile, char separator)            throws IOException {        randomAccessFile.seek(beginIndex);        long count = 0;        while (randomAccessFile.read() != separator) {            count++;        }        count++;        if (count>=gap) {            return -1;        }        return count;    }}class MyCallable implements Callable<Object> {    private int i;    private long l;    private long m;    private String path;    private RandomAccessFile randomAccessFile;    private char separator;    private String code;    private String taskNum;    private String fileTruncate;    private String date;    private HashMap<String, String> parseField;    private HashMap<String, Object> parse;    private HashMap<String, String> parseJson;    MyCallable(String taskNum) {        this.taskNum = taskNum;    }    public MyCallable(int i, long l, long m, String path, RandomAccessFile randomAccessFile, char separator, String code            ) {        this.i = i;        this.code = code;        this.l = l;        this.m = m;        this.randomAccessFile = randomAccessFile;        this.path = path;        this.separator = separator;        File file = new File(path);        String fileName = file.getName();    }    public Object call() throws Exception {        int data = readData(l, m, path, randomAccessFile, separator, code);        return data;    }    private int readData(long begin, long end, String path, RandomAccessFile randomAccessFile, char separator, String code)            throws FileNotFoundException, IOException {        // System.out.println("开始工作" + Thread.currentThread().getName());        if (randomAccessFile == null) {            randomAccessFile = new RandomAccessFile(path, "r");        }        randomAccessFile.seek(begin);        StringBuilder builder = new StringBuilder();        int i = 1;        while (true) {            String read = randomAccessFile.readLine();            long filePointer = randomAccessFile.getFilePointer();//          begin++;            if (read != null) {//                  System.out.println(new String(read.getBytes("ISO-8859-1"), code)+"==="+i);//                  System.out.println(i);                    }                     i++;                    if (filePointer >= end) {                        break;                    }                }//              builder = new StringBuilder();                randomAccessFile.close();                return 1;        }}
原创粉丝点击