Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:15:05+00:00 2026-06-13T21:15:05+00:00

I am trying to read a text file and adding each line to a

  • 0

I am trying to read a text file and adding each line to a list. The list is used in prepared statement to insert the records to db. The problem is when there is 1000000 records the execution becomes too slow and some time gets Out of Memory error.

My code

while ((sRec = in.readLine()) != null) {
        myCollection.add(reversePad(sRec, hmSizeLookup, colLength, tableName));
        a = Utility.getPositions(sRec, hmSizeLookup, colLength);
        i = i + 1;
}
for (int j = 0; j < a.length; j++) {
    Field f = (Field) hmSizeLookup.get(String.valueOf(j));
    sAllFields += f.getColumnName();
    if (j < a.length) {
        sValues += "?";
    }
    if (j < a.length - 1) {
       sValues += ",";
       sAllFields += ",";
    }
}
sQ = "insert  into " + tableName + "(" + sAllFields + ") values(" + sValues + ")";
ds1.executePreparedStatement(sQ, myCollection);

How can read only 1000 lines at a time from the text file and need to perform the insert operation ? This should continue until the end of text file.

Any suggestions are appreciated,

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:15:06+00:00Added an answer on June 13, 2026 at 9:15 pm

    You need to do the inserting in the file loop.

    Chop the insert code out to a function, every 1000 lines call it and then myCollection.clear()

    After the loop you’ll need to call it again for any data after the last insert.

    So something like:

    int line = 0;
    while ((sRec = in.readLine()) != null) {
            myCollection.add(reversePad(sRec, hmSizeLookup, colLength, tableName));
            a = Utility.getPositions(sRec, hmSizeLookup, colLength);
            i = i + 1;
        if (++line > 1000) {
            doTheInsert(myCollection);
            myCollection.clear();
            line = 0;
        }
    }
    doTheInsert(myCollection);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a text file line by line, and add each line
I am trying to read a text file line by line, and the text
I am trying to read in a text input file that contains a list
I am trying to read a list of values from a text file, hello.txt
I am trying to read text from a text file. There are some special
I'm trying to read a text file, and then break it up by each
I'm trying to read a text file line by line using InputStream from the
I'm trying to read a text file one line at a time and print
Here I'm trying to read a text file which contains only integers.in every line.For
I'm trying to read a text file using a synchronous request. It doesn't work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.