Regarding the Stream tokenizer
Hi,
i have the following code snippet.
StreamTokenizer in = new StreamTokenizer(new FileReader("C:\\setup\\diskstats.txt"));
while (in.nextToken() != StreamTokenizer.TT_EOF)
{
if(in.ttype == StreamTokenizer.TT_WORD )
{
if(in.sval.equals ("cciss/c0d0p1"))
{
Sytem.out.println("Success");
}
}
}
}
When it tries to read the following line from" C:\\setup\\diskstats.txt ":
1041 cciss/c0d0p1 4884960 11309746 4634 9268
It matches only the "cciss" from the thrid token instead of "cciss/c0d0p1".It is dicarding the content after "/". How can i get the whole string (word) "cciss/c0d0p1" to be matched.
StreamTokenizer is the wrong tool for this job. We need more information about your requirements before we can tell you what the right tool is, but here's a possible approach: BufferedReader in = new BufferedReader(new FileReader("C:\\setup\\diskstats.txt"));
String line = null;
while ((line = in.readLine()) != null)
{
String[] tokens = line.split("\\s+");
System.out.println(tokens[2]);
}
Given the line 104 1 cciss/c0d0p1 4884960 11309746 4634 9268 this will print cciss/c0d0p1 If you need to do something more complicated than just splitting on whitespace, you might want to use java.util.Scanner instead.
n.b.: split() requires JDK 1.4+, and Scanner requires JDK 1.5+
Thanks for ur reply.
Actually i am reading a kernel file "diskstats" in linux for disk information. It contains a set of lines .So,I am doing it using StreamTokenizer. Can u suggest me the best solution i can go with. it is using jdk1.2 for compilation. Also,Can know how i can i acheive this by stream tokenizer.
You can't use StreamTokenizer for this; there's no way to tell it that a "word" can contain slash characters. If you're stuck with JDK 1.2, use a StringTokenizer. You'll still want to use a BufferedReader to read the file line-by-line, as I did above. Here's how you might use the StringTokenizer: import java.util.StringTokenizer;
public class Test
{
public static void main(String[] args)
{
String line = "104 1 cciss/c0d0p1 4884960 11309746 4634 9268";
StringTokenizer st = new StringTokenizer(line);
for (int i = 0; st.hasMoreTokens(); i++)
{
String token = st.nextToken();
if (i == 2)
{
System.out.println(token);
break;
}
}
}
}