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