StreamTokenizer
StreamTokenizer
is a utility class in Java used to break an input stream into tokens, which can be either words, numbers, comment styles or single characters. It allows to read one token at a time. It is a part of the java.io
package and is particularly useful for parsing text data.
Each byte read from the input stream is regarded as a character in the range '\u0000'
through '\u00FF'
. The character value is used to look up five possible attributes of the character: white space, alphabetic, numeric, string quote, and comment character. Each character can have zero or more of these attributes.
Constructors
- StreamTokenizer(InputStream is) (Deprecated)
- StreamTokenizer(Reader r): This constructor creates a
StreamTokenizer
object that parses the characters from aReader
object. It is the most commonly used constructor as it allows reading from various character-based input sources likeFileReader
,BufferedReader
,StringReader
, etc.
import java.io.*;
public class StreamTokenizerInputStreamExample {
public static void main(String[] args) {
try {
// Using InputStream to read from a file
InputStream inputStream = new FileInputStream("/Users/praful/Desktop/query-explain-nat-ext.txt");
StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader(inputStream));
tokenizer.whitespaceChars('$', '$'); // Treat '$' as whitespace
tokenizer.ordinaryChar(' ');
while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch (tokenizer.ttype) {
case StreamTokenizer.TT_WORD:
System.out.println("Word: " + tokenizer.sval);
break;
case StreamTokenizer.TT_NUMBER:
System.out.println("Number: " + tokenizer.nval);
break;
default:
System.out.println("Character: " + (char) tokenizer.ttype);
}
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Fields
nval: If the current token is a number, this field contains the value of that number
sval: If the current token is a word token, this field contains a string giving the characters of the word token.
TT_EOF: A constant indicating that the end of the stream has been read
TT_EOL: A constant indicating that the end of the line has been read.
TT_NUMBER: A constant indicating that a number token has been read.
TT_WORD: A constant indicating that a word token has been read.
ttype: After a call to the nextToken
method, this field contains the type of the token just read.
Common Methods
int nextToken()
: Parses the next token from the input stream. Sets thettype
,nval
, orsval
fields based on the token type.void pushBack()
: Pushes the current token back so it can be read again in the next call tonextToken()
.void ordinaryChar(int ch)
: Specifies that a character should be treated as an ordinary character (i.e., it will not be treated as a delimiter).void wordChars(int low, int hi)
: Specifies that characters in the specified range should be treated as part of a word.void whitespaceChars(int low, int hi)
: Specifies that characters in the specified range should be treated as whitespace.
RandomAccessFile
RandomAccessFile
in Java is a class in the java.io
package that allows you to read from and write to a file at any position. RandomAccessFile
supports both reading and writing to a file and allows non-sequential, or random, access to the file’s contents. A random access file behaves like a large array of bytes. There is a cursor implied to the array called file pointer, by moving the cursor we do the read write operations. This makes it suitable for applications that require frequent updates to specific locations within a file, such as databases or log files.
If end-of-file is reached before the desired number of bytes has been read, an EOFException
(which is a kind of IOException
) is thrown. If any byte cannot be read for any reason other than end-of-file, an IOException
other than EOFException
is thrown. In particular, an IOException
may be thrown if the stream has been closed.
Constructors
RandomAccessFile(String name, String mode)
: Creates a random access file stream to read from and optionally to write to a file with the specified name.
RandomAccessFile(File file, String mode)
: Creates a random access file stream to read from and optionally to write to the specified file.
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileExample {
public static void main(String[] args) {
File file = new File("/Users/praful/Desktop/query-explain-nat-ext1.txt");
try {
double d = 1.5;
float f = 14.56f;
// Creating a new RandomAccessFile - "GEEK"
RandomAccessFile geek = new RandomAccessFile(file, "rw");
// Writing to file
geek.writeUTF("Hello Geeks For Geeks");
// File Pointer at index position - 0
geek.seek(0);
// read() method :
System.out.println("Use of read() method : " + geek.read());
geek.seek(0);
byte[] b = {1, 2, 3};
// Use of .read(byte[] b) method :
System.out.println("Use of .read(byte[] b) : " + geek.read(b));
// readBoolean() method :
System.out.println("Use of readBoolean() : " + geek.readBoolean());
// readByte() method :
System.out.println("Use of readByte() : " + geek.readByte());
geek.writeChar('c');
geek.seek(0);
// readChar() :
System.out.println("Use of readChar() : " + geek.readChar());
geek.seek(0);
geek.writeDouble(d);
geek.seek(0);
// read double
System.out.println("Use of readDouble() : " + geek.readDouble());
geek.seek(0);
geek.writeFloat(f);
geek.seek(0);
// readFloat() :
System.out.println("Use of readFloat() : " + geek.readFloat());
geek.seek(0);
// Create array upto geek.length
byte[] arr = new byte[(int) geek.length()];
// readFully() :
geek.readFully(arr);
String str1 = new String(arr);
System.out.println("Use of readFully() : " + str1);
geek.seek(0);
// readFully(byte[] b, int off, int len) :
geek.readFully(arr, 0, 8);
String str2 = new String(arr);
System.out.println("Use of readFully(byte[] b, int off, int len) : " + str2);
geek.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}