Search code examples
javajdbcdaodto

how to properly insert data to database from txt file


simple question: any ideas how this should be properly done?. I have 3 txt files with lots of information, I created a class that will be in charge of reading the data from the txt files and returning the data as a list of DTO components (Yes, the information can be bundle as such logic unit), depending on the txt file, after that the client will use a DAO and will use such a list and insert the data to a local database (sqlite). My concern is that having such a List could be memory demanding, should I avoid using such list and somehow insert this data using the dao object directly without bundling the data into a dto and finally such list?


Solution

  • You are asking a good question and partially answering it yourself. Yes, sure if you really have a lot of information you should not read all information from file and then store it in DB. You should read information chunk-by chunk or even if it is possible (from application point of view) line-by-line and store each line is DB.

    In this case you will need memory for one line only at any time.

    You can design the application as following.

    • File parser that returns Iterable<Row>
    • DB writer that accepts Iterable<Row> and stores rows in DB,
    • Manager that calls both.

    In this case the logic responsible on reading and writing file will be encapsulated into certain modules and no extra memory consumption will be required.