In this program, we will take a look at how to read a file and split its contents using StringTokenizer Class in Java,
File Content:Id, Name, Age, Gender
1, Sam, 22, Male
2, Rita, 32, Female
3, Alex, 21, Female
4, Kate, 22, Female
Program:
import java.io.*;
import java.util.StringTokenizer;
/**
* Read a file and split lines
* using StringTokenizer
*/
public class StringTokenizerExample {
public static void main(String[] args) throws IOException {
File csvFile = new File("/Users/c2c/input-file.csv");
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String line;
while ((line = br.readLine()) != null) {
StringTokenizer stringTokenizer = new StringTokenizer(line,",");
while (stringTokenizer.hasMoreTokens()) {
//Split the line using stringTokenizer
System.out.println(stringTokenizer.nextToken().trim());
}
}
}
}
}
Output:
Id
Name
Age
Gender
1
Sam
22
Male
2
Rita
32
Female
3
Alex
21
Female
4
Kate
22
Female
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Simple CSS Grid Example - CSS
- Android Studio : Implement abstract methods shortcut - Android-Studio
- Add Custom header and footer to Windows Notepad file - NotepadPlusPlus
- [fix] Docker: OCI runtime exec failed unable to start container process - Docker
- Turn Off Google Analytics intelligence Alert Emails - Google
- How to Search Something (string) in Android Studio Project like Eclipse - Android-Studio
- Word count in Sublime Text Editor - Sublime-Text
- PowerShell: If Else ElseIf Statements Examples - Powershell