0

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.

msagala25
  • 1,806
  • 2
  • 17
  • 24
  • 1
    Maybe you're suppressing an exception. Show your full try/catch block. And fix the compile errors. – shmosel Aug 17 '17 at 02:19
  • Hi @shmosel, I dont have try/catch in that, and there is no error occured. – msagala25 Aug 17 '17 at 02:21
  • There must be a try/catch somewhere. There's nothing wrong with the code you posted. Don't make us beg for a [mcve]. – shmosel Aug 17 '17 at 02:22
  • Do you want to count *lines* **or** do you want to count *tokens*? Please tell us what result you get, and what result you expect. Be **specific**. – Elliott Frisch Aug 17 '17 at 02:22
  • @ElliottFrisch the result I get is not consistent, I want to read per line and split the line by pipe. – msagala25 Aug 17 '17 at 02:35
  • 1
    Your code appears reasonable enough, let's talk about this inconsistency; are you writing to this file while reading? – Elliott Frisch Aug 17 '17 at 02:39
  • @ElliottFrisch No Sir, Im just reading the line `readLine()` and after that Splitting it, and then iterate the elements of String splitted so I can check if it is separated. – msagala25 Aug 17 '17 at 02:48
  • @shmosel Hi Sir, I edit my code and put the Try/Catch, and the only thing the eclipse generated for me to catch is `IOException`. – msagala25 Aug 17 '17 at 02:49
  • Are you confident an exception is not being thrown? Did you check stderr for a stack trace? – shmosel Aug 17 '17 at 02:51
  • What is `newInputStreamReader`? There isn't enough information here for us to come to a conclusive answer. It could be bad memory on your computer. It could be a bad drive controller. It could be code related. – Elliott Frisch Aug 17 '17 at 02:57

2 Answers2

0

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();
            }
        }
    }
}
Abhishek Honey
  • 645
  • 4
  • 13
-1

Shouldn't pipe delimiter be just "|" instead of "\\|"?

Try Changing your code to:

splitLine = line.split("|"); // Splitting the line using '|' Delimiter
shadow
  • 800
  • 2
  • 16
  • 33
  • @shadow your answer is plain wrong. Go check here: https://stackoverflow.com/questions/10796160/splitting-a-java-string-by-the-pipe-symbol-using-split – Tavo Aug 17 '17 at 02:24