Reputation: 382
I am trying to write a Java method that will parse out a text file one line at a time. In the following example I want to take the number at the front of the line and use that as one key value and the the FFM number as the next key field up until the alpha numeric characters which in the following cases is CH which is not part of the key sequence.
107458982 FFM00000000000713432CH
Once the keys are separated then I want to take them and make one key field.
so in the end each line will have a key field that is actually 19 bytes long. This is what the key field looks like at the end of each line. I want to push these key fields on to an ArrayList so that I can compare them to another txt file that has similar type structures. I thought that this would be easy but it is turning out a lot harder than I thought that it might be.
Key Filed --> 107458982FFM00000000000713432
My text file, in this case, has a blank line on every other line so I need to just skip a blank line which is not in my code below.
Question: How would I parse this file out as simple and quickly as possible into my key fields which will make my single key?
Code:
private ArrayList<String> scannerRead4(String inFileUsed) {
ArrayList<String> tempList = null;
try {
BufferedReader in = new BufferedReader(new FileReader(inFileUsed));
try {
String line;
while ((line = in.readLine()) != null) {
String[] s = line.split(" ");
for (int index=0;index<line.length()-1; index++) {
System.out.println("s: " + s[index]);
}
//tempList = new ArrayList<String>(Arrays.asList(line.split(" ")));
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return tempList;
}
File context that needs to be parsed
107458982 FFM00000000000713432CH
107462291 FFM00000000001835472T
107462291 FFM00000000002200570T
107462291 FFM00000000002432274T
108018296 FFM00000000001431509T
108018296 FFM00000000001553064T
108122386 FFM00000000001211063T
108122386 FFM00000000001862517T
108491927 FFM00000000004556330T
108500569 FFM00000000001682124
108500569 FFM00000000002023040
108523927 FFM00000000000611642
108523927 FFM00000000002162127
108768840 FFM00000000001360865T
108768840 FFM00000000001796191T
108774203 FFM00000000001821871T
108774203 FFM00000000001946211T
108774203 FFM00000000001914770T
Upvotes: 0
Views: 47
Reputation: 14276
Use a HashMap
if you plan to have a key/value design:
HashMap<String, String> map = new HashMap<String, String>();
Then instead of using readLine()
use read()
. You want to read a character at a time to be as efficient as possible. If you read a line at a time, then you have to parse the line String which is a waste of time. You want two separate inner loops to skip the spaces and look for the value. The outer loop will look for the key.
Also, use StringBuffer
instead of String
. This is good for when you plan to concatenate lots of Strings or characters.
This pseudo code assumes your file is correct (no missing keys or values).
StringBuffer key, value;
char c;
while ((c= in.read()) != null) {
key = new StringBuffer();
if(c != " "){
key.append(c);
}else{
value = new StringBuffer();
while ((c= in.read()) == " ");//skip all the spaces
value.append(c);//add last char found
//find rest of value until new line
//may want to use System.getProperty("line.separator") or whatever the char value of new line is.
while ((c= in.read()) != null && c != "\n") {
value.append(c);//add last char found
}
map.put(key.toString(), value.toString());//map it all together
}
}
NOTE: Treat this as pseudo code. I haven't tested it myself, but this should be a good way to do it.
Upvotes: 1