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

后端 未结 2 998
北海茫月
北海茫月 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:00

    Your doing it the hard way by not using readLine

        try {
            FileInputStream fis = new FileInputStream("F:\\Test.txt");
            reader = new BufferedReader(new InputStreamReader(fis));          
            String line = reader.readLine();
            while(line != null){
    
                //process your line here, it's just a String...   
    
                line = reader.readLine();
            }           
    
        } catch (FileNotFoundException ex) {
            ...
        } catch (IOException ex) {
    
    0 讨论(0)
  • 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.
    0 讨论(0)
提交回复
热议问题