Forums Register Login

Reading strings from file

+Pie Number of slices to send: Send
I am writing a small file processing application. It reads a file consisting of a set of lines each of which have the same format - say a string followed by a number followed by a boolean (for example "this is a string" 123 true).
How can I process the file such that I get the String, int, boolean one after the other. I tried to use DataInputStream class and getLine method, but it returns the whole line. The class has methods to read primitive types but not String. Other File classes have read method, but that returns one byte at a time.
Thanks,
Naveen
+Pie Number of slices to send: Send
Do you have a space of "fixed separator" between the different values?
+Pie Number of slices to send: Send
Yes the file would have a space or tab as a seperator.
+Pie Number of slices to send: Send
How about using RandomAccessFile (readLine) and StringTokenizer (hasMoreTokens and nextToken).
RandomAccessFile raf = new RandomAccessFile("filename", "r");
String line = raf.readLine();
StringTokenizer st = new StringTokenizer(line);
String s = st.nextToken();
You may use parse to get int from string.
Code the above in appropriate loop.
check for null for end of file.
use st.hasMoreTokens to check the end of st.
Jayakumar
+Pie Number of slices to send: Send
Hi,

But what if you encountered a null while traversing the StringTokenizer.
It wont take null as Token and thus cud mislead you.

I am facing the similiar problem.
Suppose I have one String as
WORK<tab as separator>HOME<tab as separator><tab as separator>OFFICE.

Now If I run my StringTokenizer on this String it will only return me three Tokens WORK,HOME,OFFICE.

If I wud have to insert the above tab separated String in DB, my fourth token will get inserted in third column and Last column will get Null

Please help on this issue..
Thanks ain Advance
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1468 times.
Similar Threads
SERIALIZATION IN STRUTS
Explain the strange behavior of Boolean Class??
linebreaks
Session Management - Serialization
questions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:07:38.