0

I have a text file listing a bunch of text books (its an 8mb .txt file!!) I am supposed to write script to read in the file and write the information into a database we have setup. Each book is a row in the database. The textfile looks like so:

Book_Title: some title here
ISBN: some ISBN


.........................
Book_Title: the title
(etc...)

Each book is separated by the periods, and the "Key: Value" pairs are the database column name before the colon and the value to store after the colon. How shoul I go about reading in the information? I tried using scanner and sayiying the delimiter is ":", but there is a field "Updated_On: ... at 10:30:28" .. Any other suggestions?

1 Answer 1

2

I hope the following pseudo-code will be simple enough. 8Mb is not that much so it all can be done in memory.

  1. Read file into a string
  2. Split the string on the record separator which is "........................." in your case.
  3. Iterate over the array of records, split each element in the records array on "\n", this will produce concatenated array of name/value pairs for each element in the records array.
  4. Iterate over the array of the concatenated name/value pairs and split each ":", this will produce separated arrays of names and values.

It will be memory hungry, something like O(3N), but, for files under couple of gigs it should work fairly well.

To read the file into a string you can use Apache Commons, FileUtils. FileUtils JavaDoc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.