halilkaya
halilkaya

Reputation: 467

Missing lines when reading file by Reader

I have a local JSON file in my project which called basic_questions.json. I also have a ParseJSON class to receive the content of this file.

basic_questions.json

[
    {
        "correct" : "answer2",
        "answer1" : "Yıldırım",
        "answer0" : "Şimşek",
        "question" : "Halk arasındaki adı Ebemkuşağı olan yağmurdan sonra gökyüzünde oluşan kemer biçimindeki renkli görüntüdür?",
        "answer3" : "Hortum",
        "answer2" : "Gökkuşağı"
    },
    {
        "correct" : "answer0",
        "answer1" : "Kardelen",
        "answer0" : "Yediveren",
        "question" : "Yılda birkaç kez meyve veren, çiçek açan bitki hangisidir?",
        "answer3" : "Gelincik",
        "answer2" : "Orkide"
    },
    {
        "correct" : "answer3",
        "answer1" : "Tenis",
        "answer0" : "Hokey",
        "question" : "Çim zemin üzerinde, ufak bir topa özel sopalarla vurularak belli bir deliğe sokma amaçlı oynanan oyuna ne ad verilir?",
        "answer3" : "Golf",
        "answer2" : "Hentbol"
    }
]

ParseJSON.class

try {

    bReader = new BufferedReader(new FileReader(JSON_FILE));

    while (bReader.readLine()!=null) {
        System.out.println(bReader.readLine());
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (bReader != null) bReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Output:

    {
        "answer1" : "Yıldırım",
        "question" : "Halk arasındaki adı Ebemkuşağı olan yağmurdan sonra gökyüzünde oluşan kemer biçimindeki renkli görüntüdür?",
        "answer2" : "Gökkuşağı"
    {
        "answer1" : "Kardelen",
        "question" : "Yılda birkaç kez meyve veren, çiçek açan bitki hangisidir?",
        "answer2" : "Orkide"
    {
        "answer1" : "Tenis",
        "question" : "Çim zemin üzerinde, ufak bir topa özel sopalarla vurularak belli bir deliğe sokma amaçlı oynanan oyuna ne ad verilir?",
        "answer2" : "Hentbol"
]

I have no idea about the lack of content.

Upvotes: 0

Views: 82

Answers (2)

krampstudio
krampstudio

Reputation: 3621

You read the line twice:

try {

    bReader = new BufferedReader(new FileReader(JSON_FILE));
    String line = null;
    do{
            line = bReader.readLine()
            System.out.println(line);
    }while (line!=null);

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (bReader != null) bReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Smutje
Smutje

Reputation: 18143

You call readLine() twice - one for checking and one for actual reading, so every second line is being discarded for checking.

A common pattern for this is

bReader = new BufferedReader(new FileReader(JSON_FILE));
String line;

while ((line = bReader.readLine()) != null) {
    System.out.println(line);
}

Upvotes: 4

Related Questions