In: Computer Science
#Java
I am reading from the txt file
and I put everytihng inside of the String[], like that:
String[] values = str.split(", ");
But, now I have to retrieve the data from it one by one. How can I do it?
Here is the txt file:
OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10
Thanks!
Main.java :
import java.io.BufferedReader;
import java.io.FileReader;
/*
Java Program to read file and retrieve comma separated String
*/
public class Main {
public static void main(String[] args) throws Exception {
/*
Redaing file using BufferedReader
*/
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line = null; // local variable to hold each line (it will change in each loop iteration)
// while loop execute until no more line available to read in file.
while ((line = br.readLine()) != null) {
String[] values = line.split(","); // String array to hold each comma (,) separated string
/*
follwing for each loop retrieve the data from values(String array) one by one
*/
for (String str : values) {
System.out.println(str); // print each retrieved value in a single line
}
}
br.close(); // close the BufferedReader.
}
}
Sample Output :
Please refer to the Screenshot of the code given below to understand indentation of the code :