If I have a file with some structure to it:
type
2
0 0 name
100 100 name
1
1 2 name name
How can I use a StreamTokenizer
to process this file? Is the only way the procedural approach?
i.e.
StreamTokenizer st = new StreamTokenizer(new FileReader(filename));
if (st.nextToken() != StreamTokenizer.TT_EOF) {
st.nextToken();
if (st.sval == "typea") {
st.nextToken();
int i = (int) st.nval;
if (i > 0) {
while (i > 0) {
// process node sets
}
}
} else if (st.sval == "typeb") {
st.nextToken();
int i = (int) st.nval;
if (i > 0) {
while (i > 0) {
// process node sets
}
}
}
}
It's fine to skip if unexpected tokens exist.
A great deal of the functionality of
StreamTokenizer
has been replaced by theScanner
class, I believe it's worth looking before proceeding.You will probably need to do it in a procedural fashion. Consider breaking things into separate functions and gathering your node sets as you go along. Here's something simple that parses your given example. You can build on it as things get more complicated. I'm sure someone else can offer you something better.