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

Bent C Dalager

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?

I'd start by looking into the java.io and java.sql packages and then
take it from there.

Cheers
Bent D
 
S

Steve W. Jackson

: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 =
 
S

Sudsy

H said:
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

Use a BufferedReader and the readLine method to obtain a String
containing the record. Use the substring method to "slice and dice"
the record. Since it appears as though parts of the record are
numeric, use something like Interger.parseInt to process the
substring.
On the insert side, create a PreparedStatement and loop through
setting the various fields using setInt and setString methods.
Or hire me to write the code! ;-)
 
D

Dave Monroe

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
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top