0

Hi

i have a requirement to read a text file where field positions are specified and make use of that data to insert into tables. Please refer to the data in the text file below

1234 HEDEPT 12-3-2009 12-03-2009 yesno yes A hello@hello.com 12 y12
1234 HEDEPT 12-3-2009 12-03-2009 yesno yes A hello@hello.com 12 y12

i am reading the text file using Filereader . please help me how to read the same making use of field positions.
emp id 1-4[fieldposition]
deptcode 4-12[fieldposition]

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by javaAddict
0

If you are reading the file line by line, and you are SURE that each line will have values separated by an empty String then you can use this example:

String line = "1234 HEDEPT 12-3-2009 12-03-2009 yesno yes A hello@hello.com 12 y12";

String [] tokens = line.split(" ");

// Now print the array and see what happens:
for (int i=0;i<tokens.length;i++) {
   System.out.println(i+":"+tokens[i]);
}

There is always the subString method you can use that is only when you know the the fields will have a fixed size. If the lines are like that use the split method:

1234 HEDEPT 12-3-2009 12-03-2009 yesno yes A hello@hello.com 12 y12
123 HEDEPTASD 12-3-2009 12-03-2009 yesno yes A bye@bye.com 12 y12
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.