Hi StackOverFlow People,
I have this issue in my development of System, where I have 4451 lines of record in a text file , and I am retrieving it using BufferedReader
and split every line by pipe ( | ). I'm using Quartz also to run this reading of file every day. when I test it, I set the quartz job every minute so I can test It if it actually reading the file in every minute. It reads all of the line in the text file by checking it using this.
BufferedReader reader = new BufferedReader((newInputStreamReader(inputStream));
String line = null;
int counter = 0;
while((line = reader.readLine()) != null){
counter++;
}
System.out.println(counter);
But when I split the String
, The result of retrieving 4451
records is inconsistent. Sometimes, It only retrieves 1000+ to 2000+ records, and Sometime it retrieves 4451, but not consistently. This is my code.
try {
BufferedReader reader = new BufferedReader((newInputStreamReader(inputStream));
String line = null;
int counter = 0;
String[] splitLine = null;
while((line = reader.readLine()) != null){
splitLine = line.split("\\|"); // Splitting the line using '|' Delimiter
for(String temp : splitLine) {
System.out.println(temp);
}
counter++;
}
System.out.println(counter);
} catch (IOException e) {
e.printStackTrace();
}
Is the splitting of String and Iterating of the readfile at the same time could be the cause?
EDIT: There's no Exception Occured in the Situation. It Only print the length of by using the counter
variable.
My Expected Output is I want to Retrieve all the records per line in the text file and split the string per line by pipe
. counter
is the count of lines retrieved.
I didn't find any error in your code but the code that I have written is working perfectly fine. Here is the code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Test {
public static void main(String[] args) {
FileReader inputStream = null;
BufferedReader reader = null;
try {
inputStream = new FileReader("Input.txt");
reader = new BufferedReader(inputStream);
String line = null;
int counter = 0;
String[] splitLine = null;
while ((line = reader.readLine()) != null) {
splitLine = line.split("\\|");
for (String temp : splitLine) {
System.out.println(temp);
}
counter++;
}
System.out.println(counter);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Shouldn't pipe delimiter be just "|"
instead of "\\\\|"
?
Try Changing your code to:
splitLine = line.split("|"); // Splitting the line using '|' Delimiter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.