Solved BufferedReader.readLine() returns null even though there is text in the file.

Discussion in 'Plugin Development' started by Conarnar, Nov 17, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Conarnar

    Code:java
    1.  
    2. try {
    3. File file = new File(getDataFolder(), "requests.log");
    4. if (!file.exists()) {
    5. file.getParentFile().mkdirs();
    6. file.createNewFile();
    7. }
    8. BufferedReader reader = new BufferedReader(new FileReader(file));
    9. String line;
    10. while ((line = reader.readLine()) != null) {
    11. String[] lore = line.split(" ", 2);
    12. ItemStack stack = new ItemStack(Material.WRITTEN_BOOK);
    13. BookMeta meta = (BookMeta) stack.getItemMeta();
    14. meta.setTitle(lore[0]);
    15. meta.setAuthor("PlayerLores");
    16. meta.addPage(lore[1]);
    17. stack.setItemMeta(meta);
    18. prepared.add(stack);
    19. }
    20. reader.close();
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }


    So in the while ((line = reader.readLine()) != null) loop, it doesn't run. Yes I have tested it with System.out.println(stuff);
     
  2. Offline

    felixfritz

    Hm, that's odd... It worked perfectally fine for me when I did this:
    Code:java
    1. File file = new File("plugin.yml");
    2. BufferedReader reader = new BufferedReader(new FileReader(file));
    3. String line;
    4.  
    5. while((line = reader.readLine()) != null) {
    6. System.out.println(line);
    7. }
    8.  
    9. reader.close();

    I know, you have stated that you have tested everything with the System.out.println(stuff), but I'll still ask the most basic questions and maybe the problem is hidden somewhere in there:
    • Does it even enter the method?
    • Is the file correctly spelled (also from where you add log to the log)?
    • Does it work, if you use the Scanner to read the file?
    Code:java
    1. Scanner scan = new Scanner(file);
    2. String line;
    3.  
    4. while(scan.hasNextLine()) {
    5. line = scan.nextLine();
    6. //...
    7. }
    8.  
    9. scan.close();

    These are probably all silly questions, but it might help. As for now, I can't see any problems in your code.
     
    Conarnar likes this.
  3. Offline

    Conarnar

    felixfritz
    Using Scanner works perfectly. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page