在Java中,在行首或行尾命中CSV文件

在Java中,在行首或行尾命中CSV文件,java,csv,opencsv,Java,Csv,Opencsv,我使用这段代码来分割和处理csv文件,问题是块被设置在任意位置,可能在行的开头、中间或结尾 如何将起始位置设置为行尾或行尾,使块成为完整的CSV文件而不丢失任何数据 public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); CSVReader reader = new CSVReader(new FileReader("

我使用这段代码来分割和处理csv文件,问题是块被设置在任意位置,可能在行的开头、中间或结尾

如何将起始位置设置为行尾或行尾,使块成为完整的CSV文件而不丢失任何数据

public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();

        CSVReader reader = new CSVReader(new FileReader("x_tran.csv"));
        String[] columnsNames = reader.readNext();
        reader.close();
        FileInputStream fileInputStream = new FileInputStream("x_tran.csv");
        FileChannel channel = fileInputStream.getChannel();
        long remaining_size = channel.size(); //get the total number of bytes in the file
        long chunk_size = remaining_size / 4; //file_size/threads

        //Max allocation size allowed is ~2GB
        if (chunk_size > (Integer.MAX_VALUE - 5))
        {
            chunk_size = (Integer.MAX_VALUE - 5);
        }

        //thread pool
        ExecutorService executor = Executors.newFixedThreadPool(4);

        long start_loc = 0;//file pointer
        int i = 0; //loop counter
        boolean first = true;
        while (remaining_size >= chunk_size)
        {
            //launches a new thread
            executor.execute(new FileRead(start_loc, toIntExact(chunk_size), channel, i, String.join(",", columnsNames), first));
            remaining_size = remaining_size - chunk_size;
            start_loc = start_loc + chunk_size;
            i++;
            first = false;
        }

        //load the last remaining piece
        executor.execute(new FileRead(start_loc, toIntExact(remaining_size), channel, i, String.join(",", columnsNames), first));

        //Tear Down
        executor.shutdown();

        //Wait for all threads to finish
        while (!executor.isTerminated())
        {
            //wait for infinity time
        }
        System.out.println("Finished all threads");
        fileInputStream.close();


        long finish = System.currentTimeMillis();
        System.out.println( "Time elapsed: " + (finish - start) );
    }

您可以读取该文件一次,然后使每个线程处理的行数与线程数成模(例如,第一个线程处理的行数为0、4、8等)

包。。。;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.util.List;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
公共类CsvParallelReader{
私有静态最终整数线程数=4;
公共静态void main(字符串[]args){
ExecutorService executor=Executors.newFixedThreadPool(线程编号);
试一试{
列表行=Files.readAllLines(Path.of(“yourfile.csv”);
对于(int i=0;i
您想并行读取文件还是并行处理数据?事实上,我想到了两种方法,但开始位置问题(指针)阻止了我。最终,我需要尽可能快地处理该文件(~450MB)
package ...;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CsvParallelReader {

    private static final int THREAD_NUMBER = 4;

    public static void main(String[] args) {


        ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUMBER);


        try {
            List<String> lines = Files.readAllLines(Path.of("yourfile.csv"));

            for (int i = 0; i < THREAD_NUMBER; i++) {
                Runnable readTask = new ReadTask(i, lines);
                executor.submit(readTask);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static class ReadTask implements Runnable {

        private final List<String> lines;
        private int start;

        public ReadTask(int start, List<String> lines) {
            this.start = start;
            this.lines = lines;
        }

        @Override
        public void run() {
            for (int i = start; i < lines.size(); i += THREAD_NUMBER) {
                // do something with this line of data
            }
        }
    }
}