How to split the large size .txt file data into small portion and insert into database?

后端 未结 2 1001
北海茫月
北海茫月 2021-01-25 01:35

Below is my code to read and split the text file content.

  try {

        br = new BufferedReader(new FileReader(\"F:\\\\Test.txt\"));
        final char[] cbuf         


        
2条回答
  •  孤街浪徒
    2021-01-25 02:12

     try (Scanner read = new Scanner(new File("/tmp/datafile.txt"));) {
            read.useDelimiter("@");
            while (read.hasNext()) {
                String splitedPacket = read.next();
                System.out.println(splitedPacket);
                // Perform DB Operation
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
    1. Dont read the entire file.
    2. Use Scanner to read and hold only one read @ any give time - to avoid OOM
    3. If Possible Perform Db Operation as bulk . If you are running in one transaction, read 1000 records and process as separate bulk insert.

提交回复
热议问题