Reading in a space filled file
I am reading in a file which is in byte format and each record is 50 chars wide and space-filled. If an element is not present in the file, then it is just space filled.
Example input.prn file:
Bob牋牋? Smith牋牋?2 North Rd牋牋燚enver牋牋燙O牋牋燯SA牋牋?2435
John牋 Williams牋牋?23 State St牋牋燦ew York牋牋燦Y牋牋牋牋牋12435
Notice in the second record, that John Williams does not have a country. It is just space-filled (50 chars).
So I need to read in this file, and send it to a constructor in this format:
Bob,Smith,12 North Rd,Denver,CO,USA,12435
John,Williams,123 State St,New York,NY,,12435
in the second record where there was no country, a "," needs to be in it's place.
BufferedReader br =new BufferedReader(new FileReader("input.prn"));
String line =" ";
// the delim needs to be space filled somehow
while((line = br.readLine()) !=null)
{
StringTokenizer st =new StringTokenizer(line,delim);
}
I'm not sure how to approach this problem. Any help would be greatly appreciated.
thank you
[1393 byte] By [
drakestera] at [2007-10-2 20:07:22]

I believe what I want to do is make each token 50 chars and then read each token in 50 char chunks so that it will take into account something like 14 North St and read that as one token as opposed to three different tokens (14, North, and St). I also need to take into account a 50 char token that is just empty (space-filled) and replace that with a comma.
I'm not sure how to do this.
Hi,Take a look at the method:read(char[] cbuf, int off, int len) In BufferedReader.Kaj
kajbja at 2007-7-13 22:47:43 >

I've searched but was having a problem finding an example of using
read(char[] cbuf, int off, int len)
I what context what it be used in the code below:
BufferedReader br = new BufferedReader(new FileReader("input.prn"));
String line = " ";
// the delim needs to be space filled somehow
while((line = br.readLine()) != null)
{
}
I've searched but can't find an example where
read(char[] cbuf, int off, int len)
is being implemented. If anyone could provide an example I would really appreciate it! I've tried it in the code many ways but I keep getting errors.
thank you.
char[] data = new char[50];
int len = 0;
while ((len = br.read(data, 0, data.length)) != -1 ) {
//You do now have len characters available in the data.
}
Kaj
kajbja at 2007-7-13 22:47:43 >

Kaj, I really appreciate your help. But I'm not quite sure how you then get the Strings from the input file?
char[] data = new char[50];
int len = 0;
while ((len = br.read(data, 0, data.length)) != -1 ) {
//You do now have len characters available in the data.
String line = br.readLine(); // this doesn't work
}
The condition in the while loop puts the characters read into the array names data. There is then a String constructor which looks like this:String(char[] value, int offset, int count) You can use that in the body of the loop.Kaj
kajbja at 2007-7-13 22:47:43 >

What about a situation where the total length of the line is 100 chars, but you have to get each individual String which with different max length totals?
Example:
BobSmith12 North RdDenverCOUSA12435
First element (Bob) has a max length of 10 chars.
Second Element (Smith) has a max length of 20 chars.
char[] data = new char[80];
int len = 0;
while ((len = br.read(data, 0, data.length)) != -1 ) {
//You do now have len characters available in the data.
String newstring = new String(data, 0, data.length);
}
What? Don't you have a fixed width fields? I thougth you had fixed width fields, where each field was padded with spaces if the data was shorter than the field width.Kaj
kajbja at 2007-7-13 22:47:43 >

Yes, I do have a fixed width field, but each field has a different fixed width. The total length of the line is 100 chars, but each individual field has a different fixed width:- first name is 10 chars- last name is 15 chars- address is 20 charsetc...
Then change the number 50 to 100 in the code which I posted. Your string will then contain one whole record, and you can split it into the different parts using String.substring. Another option is to change the code which I posted so that it doesn't read a whole record, but instead reads each field (you will in that case need several char arrays, one for each field.
Kaj
kajbja at 2007-7-13 22:47:43 >

char[] data = new char[80];
int len = 0;
while ((len = br.read(data, 0, data.length)) != -1 ) {
//You do now have len characters available in the data.
line = new String(data, 0, data.length);
String firstName = line.substring(0,10);
String lasttName = line.substring(10,27);
String startDate = line.substring(27,35);
String address1 = line.substring(35,45);
String address2 = line.substring(45,55);
String city = line.substring(55,65);
String state = line.substring(65,67);
String country = line.substring(67,70);
String zip = line.substring(70,80);
String record = firstName+lasttName+startDate+address1+address2+city
+state+country+zip;
}
This is the code I have which is "partially" working meaning that it is reading in the first record (Bob Smith) fine and splitting it up correctly, but the second (Tom Jones) is not being split up correctly. Would anything in the code tell you why? As I'm not sure....
BobSmith12 North RdDenverCO USA12435
TomJones82 State StMiamiFL USA95764
thanks so much
> This is the code I have which is "partially" working
> meaning that it is reading in the first record (Bob
> Smith) fine and splitting it up correctly, but the
> second (Tom Jones) is not being split up correctly.
You have to be a bit more detailed than that. What happens? What do you expect to happen? Do you get an exception?
Kaj
kajbja at 2007-7-13 22:47:43 >

while ((len = br.read(data, 0, data.length)) != -1 ) {
//You do now have len characters available in the data.
line = new String(data, 0, data.length);
String firstName = line.substring(0,10) + ",";
String lastName = line.substring(10,27) + ",";
String startDate = line.substring(27,35) + ",";
String address1 = line.substring(35,45) + ",";
String address2 = line.substring(45,55) + ",";
String city = line.substring(55,65) + ",";
String state = line.substring(65,67) + ",";
String country = line.substring(67,70) + ",";
String zip = line.substring(70,80);
String record = firstName+lastName+startDate+address1+address2+city
+state+country+zip;
System.out.println(record.toString());
}
If I add the + "," onto the String values to output comma-seperated values, the results for the second record appear incorrectly as below:
Bob,Smith,20051018,12 North Rd ,Apt 1,Miami,FL,usa,87782
Tom
Jon,es200,307308 S,tate St Apt, 12Boi,seIDu,sa,112,35
Tom
The output for the first record of Bob Smith is absolutely correct, but the record for Tom Jones is not. All of the commas are misplaced.
> The output for the first record of Bob Smith is
> absolutely correct, but the record for Tom Jones is
> not. All of the commas are misplaced.
Looks like they are off by two, and that is probably caused by incorrect data. I guess that each of your lines are 82 characters, and not 80. The records in the file are probably delimited by a carriage return and a newline character, so you have to read them as well.
Kaj