By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,493 Members | 3,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,493 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"H Brown New To It" <br*****@dhhs.state.sc.us> wrote in message
news:5b**************************@posting.google.c om...
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

Jul 17 '05 #2

P: n/a
"Hrvoje Somun" <hr****************@zg.tel.hr> wrote in message news:<bo**********@sunce.iskon.hr>...
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
Jul 17 '05 #3

P: n/a

"delusion" <my*******************@yahoo.co.uk> wrote in message
news:e2**************************@posting.google.c om...
"Hrvoje Somun" <hr****************@zg.tel.hr> wrote in message news:<bo**********@sunce.iskon.hr>...
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.

<snip>
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
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

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
Jul 17 '05 #4

P: n/a
sorry for fast typing and mistakes
(btw no, i don t have dyslexia) :)
Jul 17 '05 #5

P: n/a
On Thu, 6 Nov 2003 16:19:29 +0100, "Hrvoje Somun"
<hr****************@zg.tel.hr> wrote or quoted :
sorry for fast typing and mistakes
(btw no, i don t have dyslexia) :)


see http://mindprod.com/jgloss/newsreader.html

Agent has a spell checker.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.