Connecting Tech Pros Worldwide Forums | Help | Site Map

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

H Brown New To It
Guest
 
Posts: n/a
#1: Jul 17 '05
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

Steve W. Jackson
Guest
 
Posts: n/a
#2: Jul 17 '05

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


In article <5b14e7f1.0311041355.22d518e3@posting.google.com >,
brownha@dhhs.state.sc.us (H Brown New To It) wrote:
[color=blue]
>: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[/color]

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
Roedy Green
Guest
 
Posts: n/a
#3: Jul 17 '05

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


On 4 Nov 2003 13:55:49 -0800, brownha@dhhs.state.sc.us (H Brown New
To It) wrote or quoted :
[color=blue]
>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[/color]

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.
Dave Monroe
Guest
 
Posts: n/a
#4: Jul 17 '05

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


brownha@dhhs.state.sc.us (H Brown New To It) wrote in message news:<5b14e7f1.0311041355.22d518e3@posting.google. com>...[color=blue]
> 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
>[/color]

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
Closed Thread