1

I have a bufferedReader which should read one line of a text file (which is encoded), decode it, and check whether it contains a particular set of numbers. The part where it checks to see whether it contains a particular set of numbers is fine, however, I have a problem that it reads through the entire file, my code below, as well as the output from what is read from the file as well as how it should be read is provided below:

Code:

prviate static final String UTF8_BOM = "\uFEFF";
String cardNumberStr = "106382076";
String lineFromFile = null;
lineFromFile = bufferedReaderToWrite.readLine();
if (lineFromFile.startsWith(UTF8_BOM)) {
    lineFromFile = lineFromFile.substring(1);
}
lineFromFile = lineFromFile.substring(1, lineFromFile.length()-1);  //Convert bytes read back to String
for(String s: lineFromFile.split(", ")){    //Split every byte on ,
    sb.append((char) Integer.parseInt(s));  //Convert byte to char
}
String textToCheck = sb.toString(); //Convert StringBuilder to String
System.out.println(textToCheck);
System.out.println();
System.out.println(cardNumberStr);
if(textToCheck.contains(cardNumberStr)){
    System.out.println(lineFromFile);
}

Output from what is read from file:

106382076
78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 52,
48, 52, 54, 48, 52, 57, 51, 57, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 9
7, 108, 97, 110, 99, 101, 32, 51, 49, 48, 32, 111, 118, 101, 114, 100, 114, 97,
102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102,
116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 50, 50, 50
Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0
pin 2222Name account cardNo 106382076 Current Balance 132 overdraft false overDr
aftLimit 0 pin 4444Name test cardNo 404604939 Current Balance 310 overdraft fals
e overDraftLimit 0 pin 2222Name account cardNo 106382076 Current Balance 132 ove
rdraft false overDraftLimit 0 pin 4444`

It should output:

Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0
pin 2222

Name account cardNo 106382076 Current Balance 132 overdraft false overDr
aftLimit 0 pin 4444

The contents of the text file is as folows:

[78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 49, 56, 57, 52, 57, 51, 50, 56, 52, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 57, 52, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 51, 50, 51]
[78, 97, 109, 101, 32, 116, 101, 100, 116, 32, 99, 97, 114, 100, 78, 111, 32, 55, 48, 49, 55, 50, 53, 50, 51, 49, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 50, 54, 48, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 50, 50, 50]`
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • possible duplicate of [How to Read 1st line of a file with BufferedReader?](http://stackoverflow.com/questions/9431970/how-to-read-1st-line-of-a-file-with-bufferedreader) – Sufiyan Ghori Dec 23 '14 at 12:11
  • what according to you is 1 line the file? – varun Dec 23 '14 at 12:13
  • 2
    if i got you correct, then you have to make sure that the file is not a 1 line data, which means it should have line delimiter (\r, \n...) – Yazan Dec 23 '14 at 12:14
  • `[78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 49, 56, 57, 52, 57, 51, 50, 56, 52, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 57, 52, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 51, 50, 51]` Which is translated to `Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0 pin 2222` –  Dec 23 '14 at 12:15
  • 1
    your file needed to be break into lines in order to read just first line, I am sure your entire file is just a single line of text without any line breaks. You can first load the file, break it into lines ac to your requirement and then read the first line from that. – Sufiyan Ghori Dec 23 '14 at 12:15
  • When you _write_ to a file, do you remember to `writer.newLine()` before you write another line? Also, what do you mean by "encoded" anyway? ALL text written to a file is written using a character coding, by definition – fge Dec 23 '14 at 12:22

3 Answers3

1

I believe the problem here is that when you write to your file, you forget to include newlines...

(Also, what use do you have of the BOM?)

When you write to it and use a BufferedWriter, do NOT forget to writer.newLine() after you have written one line.

Note that you can use Files.write() to write a set of lines to a file directly, this will insert newlines for you. Finally, you can use Files.readAllLines() to read all files from a line at once.

With Java 8 you also have Files.lines().

fge
  • 119,121
  • 33
  • 254
  • 329
0

You can use BufferedReader.readLine() to get the first line.

Note that the next call to readLine() will get you the 2nd line, and the next the 3rd line....

EDIT :

If you want to specify a specific line, as your comment suggest - you might want to use Apache Commons FileUtils, and use: FileUtils.readLines(). It will get you a List which you can handle like any list, including getting a specific line. Note that it has more overhead because it reads the entire file, and populates a List with its lines.

Vignesh Shiv
  • 1,129
  • 7
  • 22
0

BufferReader readLine Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

When you have multiply lines, you should put your code in a loop like this to read each line one by one:

public static void main(String[] args) throws IOException {
    BufferedReader bufferedReaderToWrite = new BufferedReader(
        new FileReader(new File("D:/tmp/Z_Z_DELETE_ASAP/file.txt")));

    while ((lineFromFile = bufferedReaderToWrite.readLine()) != null) {
        StringBuilder sb = new StringBuilder();
        if (lineFromFile.startsWith(UTF8_BOM)) {
            lineFromFile = lineFromFile.substring(1);
        }
        lineFromFile = lineFromFile.substring(1, lineFromFile.length() - 1);  //Convert bytes read back to String
        for (String s : lineFromFile.split(", ")) {    //Split every byte on ,
            sb.append((char) Integer.parseInt(s));  //Convert byte to char
        }
        String textToCheck = sb.toString(); //Convert StringBuilder to String
        System.out.println(textToCheck);
        System.out.println();
        System.out.println(cardNumberStr);
        if (textToCheck.contains(cardNumberStr)) {
            System.out.println(lineFromFile);
        }
        System.out.println();
    }
}

Then the output will be:

Name test cardNo 189493284 Current Balance 94 overdraft false overDraftLimit 0 pin 2323

106382076

Name tedt cardNo 701725231 Current Balance 260 overdraft false overDraftLimit 0 pin 2222

106382076
Alexander Arutinyants
  • 1,619
  • 2
  • 23
  • 49