Java HELP: How do you read in a Text File (Fixed Length)

  • Thread starter H Brown New To It
  • Start date
H

H Brown New To It

I am very new to Java. How would you go about making Java program that
reads in a fixed length text file (myfile.txt) and pass the data into
a table in an sql table?

The text file data looks something like this:

123456John Smith Acme Boxes Inc 100 Someplace Drive
222222Sarah E. ConnorCyberdyne Systems Corp 1 Connorsbane Plaza
333333Martin Fry Care Of: Joseph P Helstrom200 Hive Avenue



Any help would be very appreciated.

Harvey
 
H

Hrvoje Somun

1.if all rows and all data (IDno, Name, Company, Adress) are equal length
then you can do it easily if you make a loop for1 to last IDno and substring
data.

or

2. put some delimiter beetween data (i.e. |) then you have

123456|John Smith |Acme Boxes Inc |100 Someplace Drive|
222222|Sarah E. Connor|Cyberdyne Systems Corp |1 Connorsbane Plaza|
333333|Martin Fry |Care Of: Joseph P Helstrom|200 Hive Avenue|

now with stringtokenizer go thru your file.

or

3. use XML :)

Hrvoje J. Somun
 
D

delusion

Hrvoje Somun said:
1.if all rows and all data (IDno, Name, Company, Adress) are equal length
then you can do it easily if you make a loop for1 to last IDno and substring
data.

or

2. put some delimiter beetween data (i.e. |) then you have

123456|John Smith |Acme Boxes Inc |100 Someplace Drive|
222222|Sarah E. Connor|Cyberdyne Systems Corp |1 Connorsbane Plaza|
333333|Martin Fry |Care Of: Joseph P Helstrom|200 Hive Avenue|

now with stringtokenizer go thru your file.

I agree with the first method even though you didn't tell him any file
stuff ie. how to open the file how to read through it etc.

but ...
i'm intrigued
you propose
or

3. use XML :)

as a solution
please tell .... how would you use xml to do something like this and
integrate it into java?

please tell me
 
H

Hrvoje Somun

delusion said:
"Hrvoje Somun" <[email protected]> wrote in message
I agree with the first method even though you didn't tell him any file
stuff ie. how to open the file how to read through it etc.
yes, but that part is in any java tutorial

but ...
i'm intrigued
you propose


as a solution
please tell .... how would you use xml to do something like this and
integrate it into java?

please tell me


if he wants to insert data from *.txt to rational database and use java as a
tool to do it he can use xml file instead of txt file, he sould transform
his txt file into smoething like;
<customer ID="123456">
<name>John Smith</name>
<company>Acme Boxes Inc</comapny>
<adress>100 Someplace Drive</adress>
</customer>

use some parser (if all his data about customers is in a single file then
SAX), and insert data into database.
?
i did it few times.
yes, if he cant change txt file then this way is not good, but if he is just
starting to build hiss application he should use this metod(XML has many
other advantages.....)


Hrvoje J. Somun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top