Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with file reading in

 
Stephanie Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a file with an output like this:
1> 85 25 ee 58 ef 56 36
2> 45 ee 56 41 25 2c 36
.
.
.
10> 45 ee 56 41 25 2c 36
11> 25 ef ee 25 36 21 de
.
.
.
i am trying to store the packet bytes (85,25,ee...) to compare to other values. but i'm not sure how to get past the numbering part (1>,2>,3>...) before each line. currently i am using a FileInputStream to read in the data.
while ((pktCharInt = fis.read()) != -1)
{
do
{
pktChar = (char)pktCharInt;

a[i] = pktChar;

pktCharInt = fis.read();
pktChar = (char)pktCharInt;

i++;
a[i] = pktChar;
i = 0;

pktCharInt = fis.read();

if (pktCharInt == -1)
break;

pktChar = (char)pktCharInt;

}
while(pktChar != ' ');

pktByte = new String(a);

}
i am using a length 2 array to store the bytes. but i don't know how to skip over the numbers in the beginning. any kind of help is appreciated.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephanie, probably could have continued this in the other thread you started about this file. It would keep things together and keep clutter down.
If you want to bypass the 1>, instead of dealing with each character I would still use StringTokenizers. Use one StringTokenizer that breaks things up based on a ">" token. The second String you get back from that tokenizer will be the long list of 0A EE... etc. from each line. Truncate the String to get rid of the space at the beginning. Then, use a new tokenizer on that String.
Check out the API for StringTokenizer to see how to create one with a specific token.
 
Stephanie Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jason,
i saw your reply and am trying to implement it the way you said. however, i'm still confused on how to store the first toke and then the second as another. right now it prints out
1
85 25 ee 58 ef 56 36
2
45 ee 56 41 25 2c 36
.
.
.
String thisLine = null;

BufferedReader br = new BufferedReader(new FileReader(ExtractionGUI.file));
BufferedWriter bw = new BufferedWriter(new FileWriter(fileOut));

while ((thisLine = br.readLine()) != null)
{
StringTokenizer token = new StringTokenizer(thisLine, ">");
while (token.hasMoreTokens())
{
bw.write((token.nextToken()).trim());
bw.newLine();

}
}
but now i only want to parse the "85 25 ee 58 ef 56 36"
"45 ee 56 41 25 2c 36" and ignore the sequential numbering. so that would be every other token but i'm not sure how to implement that with another String Tokenizer. any help is much appreciated!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you bw.write() line, do a call to token.nextToken() just ignore the results.
So you would have
StringTokenizer token = new StringTokenizer(thisLine, ">");
while (token.hasMoreTokens())
{
token.nextToken(); //skips the first token which will be your numbering
bw.write((token.nextToken()).trim());
bw.newLine();
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I waited a couple days to think about this. Sounds like you have the problem solved so this is just something to chew on.
PJ Plauger wrote a bunch of wonderful essays on programming in the 80s. One essay said some programs are shaped like their inputs. This input has tokens within lines, and this algorithm has token processing within line processing:

That code tells us about lines and word tokenizers. Is that important? Maybe not. Maybe lines and word spacing are just coincidence and this describes our intent better:

What does getNextToken do? We don't know (yet) and really don't care. We could change it to read arrays or databases or streams or web sites without affecting this little method.
I'm not sure where I was going to go with all this. Does it give you some ideas about how to look at code? "Goodness" in code is highly subjective and you might even like the original better. Or maybe it will give you new eyes to see something new hiding in your code.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pulling all of the tokenizing into a method is a good idea, especially if there may ever be a need to change the format of the file. For instance, what if the line numbering system changes, or is done away with all together? Or, as Stan mentions, maybe the file isn't used at all in favor of some other mechanism.
Plus it makes the code look cleaner than having all those tokenizings going on in loop. Once you get the jist of how to break apart your Strings, wouldn't be a bad idea to see how you could pull all of it into a method(s).
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!