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

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 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+
3 Replies


P: n/a
In article <5b**************************@posting.google.com >,
br*****@dhhs.state.sc.us (H Brown New To It) wrote:
:I am very new to Java. How would you go about making Java program that
:reads in a fixed length text file 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


One "simple" way would be to get a File object representing the file,
then pass that to the constructor of a FileReader object, and that in
turn to the constructor of a LineNumberReader object. That part might
look something like this:

File myFile = new File("thefilepath");
LineNumberReader lnr = new LineNumberReader(new FileReader(myFile));

Then you can read each line in turn into a String and process it as
needed. Something like:

String line = null;

line = lnr.readLine();
while (lineText != null) {
// do something with the line here...
line = lnr.readLine();
}

When done, do lnr.close() to close the reader. This omits exception
handling you'll be required to do, etc. But that's a nutshell approach
to reading a text file. In your code that handles the individual lines,
you can deal with your existing knowledge of where individual "fields"
are located.

And a disclaimer: this is not the only way, just a way you could do
this that's relatively simple.

HTH.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Jul 17 '05 #2

P: n/a
On 4 Nov 2003 13:55:49 -0800, br*****@dhhs.state.sc.us (H Brown New
To It) wrote or quoted :
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


these are lines of data, terminated with CrLf right?

see http://mindprod.com/fileio.html for how to read them.

You can then extract the fields with substring.

From there you can use JDBC to feed them to a database.

see http://mindprod.com/jgloss/jdbc.html
--
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 #3

P: n/a
br*****@dhhs.state.sc.us (H Brown New To It) wrote in message news:<5b**************************@posting.google. com>...
I am very new to Java. How would you go about making Java program that
reads in a fixed length text file 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


In a nutshell:

// open the file
FileInputStream fis = new FileInputStream("filename");
// apply the Reader adapter class so you can use the BufferedReader
InputStreamReader isr = new InputStreamReader(fis);
// apply the BufferedReader
BufferedReader br = new BufferedReader(isr);

String record;

while((record=br.readLine()) != null) { // null indicates EOF
// do something with the record
}

The JDBC stuff is left as an exercise for the alert reader.

Dave Monroe
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.