Skip to content

Commit 3bdbe95

Browse files
committed
feat(TokenizeFileDemo): add program to tokenize file contents using StringTokenizer
What - Added TokenizeFileDemo in Package1. - Reads raw text data from a file. - Uses `StringTokenizer` with delimiters `"= "` to split tokens. - Prints each extracted token line by line. Why - Demonstrates how to parse configuration-style text files where data is separated by `=` or whitespace. - Useful for simple parsing tasks without external libraries. - Helps in breaking raw file input into manageable tokens for processing. How - Opened a file using `FileInputStream`. - Read available bytes into a byte array and converted into a String. - Created `StringTokenizer` with delimiters `= ` (equals sign and space). - Iterated with `hasMoreTokens()` and `nextToken()` to print tokens one by one. Logic - `FileInputStream` loads file bytes into memory. - `new String(b)` reconstructs the file’s textual content. - `StringTokenizer` splits the string wherever it finds `=` or space. - Iteration collects each token until no more remain. Real-life applications - Parsing `.properties` or configuration files. - Splitting command-line input or key-value data. - Quick preprocessing of logs, metadata, or user input. Summary - TokenizeFileDemo reads a text file and tokenizes its content. - Splits data using delimiters `= ` into key/value-like tokens. - Demonstrates file I/O + string parsing in Java. Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent 8706fc4 commit 3bdbe95

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

Section25CollectionFramework/src/StringTokenizerDemo/TokenizeFile.txt renamed to Section23JavaIOStreams/String Tokenizer/src/Package1/TokenizeFile.txt

File renamed without changes.

Section25CollectionFramework/src/StringTokenizerDemo/TokenizeFileDemo.java renamed to Section23JavaIOStreams/String Tokenizer/src/Package1/TokenizeFileDemo.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package StringTokenizerDemo;
1+
package Package1;
22

33
import java.io.FileInputStream;
44
import java.util.StringTokenizer;
55

66
public class TokenizeFileDemo {
7-
public static void main(String[] args)throws Exception
8-
{
9-
//First Read the data from file.
10-
FileInputStream fis = new FileInputStream("C://Users\\somes\\Downloads\\JAVA SE\\Section25CollectionFramework\\src\\StringTokenizerDemo\\TokenizeFile.txt");
7+
public static void main(String[] args)throws Exception {
8+
// First Read the data from a file.
9+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/String Tokenizer.txt/src/Package1/TokenizeFile.txt");
1110

1211
byte b[]=new byte[fis.available()];
1312

@@ -16,10 +15,9 @@ public static void main(String[] args)throws Exception
1615
StringTokenizer stk = new StringTokenizer(data,"= ");
1716

1817
String s;
19-
while (stk.hasMoreTokens())
20-
{
18+
while (stk.hasMoreTokens()) {
2119
s=stk.nextToken();
2220
System.out.println(s);
2321
}
2422
}
25-
}
23+
}

0 commit comments

Comments
 (0)