33

EDIT for further readers: the problem was that my input file was corrupted.

I don't understand what I'm doing wrong :

I was using this code :

    File f = new File("C:\\Temp\\dico.txt");
    BufferedReader r = null;
    try {
        r = new BufferedReader(new FileReader(f));
        String scan;
        while((scan=r.readLine())!=null) {
            if(scan.length()==0) {continue;}
            //treatment
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if(r!=null) try {
            r.close();
        } catch (IOException ex) {
            Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Which is working fine. Now, for some reason, I wanted to change for a Scanner. My code became :

    File f = new File("C:\\Temp\\dico.txt");
    Scanner r = null;
    try {
        r = new Scanner(f);
        String scan;
        while(r.hasNextLine()) {
            scan = r.nextLine();
            if(scan.length()==0) {continue;}
            //treatment
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if(r!=null) r.close();
    }

This time, we never enter the while, because r.hasNextLine() always returns "false". Any idea about what I'm doing wrong ?

I precise that nothing else changed, the file is still the same.

EDIT : I also precise that I tried with another file and got the same result, meaning it doesn't comes from the file apparently.

The file looks like this :

a
à
abaissa
abaissable
abaissables
abaissai
abaissaient
abaissais
abaissait
...

Edit 2 : The content of the file seems to be problematic since the problem persists only if I copy/paste the content from my file to another. In clear, if I write the content myself, it works, if I use a part of the content of my dico.txt file, it doesn't work. Any explanation ?

Edit 3 : These are the links to my files. I advice you to avoid the dico.txt which is very huge.

dico.txt : https://drive.google.com/file/d/0B0sroFy9HZlBNDl3MUwzVnh6VU0/edit?usp=sharing

test.txt : https://drive.google.com/file/d/0B0sroFy9HZlBemZjbXU1RmlmdjQ/edit?usp=sharing

Sharcoux
  • 4,282
  • 4
  • 29
  • 65
  • Could you show the `dico.txt` file ? – Alexis C. Dec 01 '13 at 10:44
  • I edited my question to include it. But anyway, why would the BufferedReader be able to read the line correctly and not the Scanner ? Did I missed something in the initialization ? – Sharcoux Dec 01 '13 at 13:34

5 Answers5

23

This code reads the file line by line.

public static void readFileByLine(String fileName) {
  try {
   File file = new File(fileName);
   Scanner scanner = new Scanner(file);
   while (scanner.hasNext()) {
    System.out.println(scanner.next());
   }
   scanner.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } 
 }

You can also set a delimiter as a line separator and then perform the same.

 scanner.useDelimiter(System.getProperty("line.separator"));

You have to check whether there is a next token available and then read the next token. You will also need to doublecheck the input given to the Scanner. i.e. dico.txt. By default, Scanner breaks its input based on whitespace. Please ensure that the input has the delimiters in right place

UPDATED ANSWER for your comment:

I just tried to create an input file with the content as below

a
à
abaissa
abaissable
abaissables
abaissai
abaissaient
abaissais
abaissait

tried to read it with the below code.it just worked fine.

 File file = new File("/home/keerthivasan/Desktop/input.txt");
     Scanner scr = null;
         try {
            scr = new Scanner(file);
            while(scr.hasNext()){
                System.out.println("line : "+scr.next());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ScannerTest.class.getName()).log(Level.SEVERE, null, ex);
        }

Output:

line : a
line : à
line : abaissa
line : abaissable
line : abaissables
line : abaissai
line : abaissaient
line : abaissais
line : abaissait

so, I am sure that this should work. Since you work in Windows ennvironment, The End of Line (EOL) sequence (0x0D 0x0A, \r\n) is actually two ASCII characters, a combination of the CR and LF characters. if you set your Scanner instance to use delimiter as follows, it will pick up probably

 scr = new Scanner(file);
 scr.useDelimiter("\r\n");

and then do your looping to read lines. Hope this helps!

Keerthivasan
  • 12,040
  • 2
  • 26
  • 49
  • Sorry, I tried with and without the scanner.useDelimiter, and in both cases the scanner.hasNext() returns false. Sorry for my previous comment saying scanner.hasNext passed. – Sharcoux Dec 01 '13 at 13:15
  • Ok, very weird. I tried your code, it didn't work. Then, I just rewrote the file manually (no copy/paste), and this time it worked. Then, I erased the content with a copy/paste from my file and it doesn't work. It means that the content from my file probably has a problem. But in this case, why does it work with BufferedReader ? Anyway, I still have a problem since my file is composed of thousands of lines and I can't rewrite everything ! – Sharcoux Dec 02 '13 at 09:10
  • BufferedReader.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.But, Scanner depends entirely on the delimiters say space or the custom delimiter you choose. Did you try setting the delimiter with '\r\n' – Keerthivasan Dec 02 '13 at 09:16
  • Can you tell me the encoding of your input file? – Keerthivasan Dec 02 '13 at 09:25
  • Try to remove the formatting and special characters from the input file. Where do you actually copy it from? First of all, try to paste the content to Notepad and then to your input file. it really helps to find the unnecessary characters - Text sanitizer – Keerthivasan Dec 02 '13 at 09:31
  • I tried \n, \r and \r\n with no result. Sorry for being a noob, but how can I know the encoding ? Anyway, I'm only working with the notepad since the beginning. What do you want me to do exactly ? I already copied from dico.txt to test.txt and it didn't work better (cf my 2nd Edit) – Sharcoux Dec 02 '13 at 10:01
  • Can you give a link to your file somewhere? I can take a look at it. Since you are working on a windows environment, try opening the file in command prompt. which version of operation system? – Keerthivasan Dec 02 '13 at 10:15
  • You make use of Notepad++ or PSPad to view the special characters and EOL characters and try removing the unnecessary characters. – Keerthivasan Dec 02 '13 at 10:28
  • Ok. You can get the files from my 3rd edit. I checked on notepad++. There is CR and LF at the end of each line. – Sharcoux Dec 02 '13 at 12:21
  • 1
    Your dico.txt file was corrupted. I just encoded it to UTF-8 again. it worked perfectly fine with my code. I am just uploading the new file. Make use of it. i am sharing the link - https://drive.google.com/file/d/0B-0YRK_ObuLVUUozV29fdmhqaVU/edit?usp=sharing Please use this file from now on. Please let me know what happens – Keerthivasan Dec 02 '13 at 13:09
  • Ok ! The new file worked ! Thanks a lot. But I still don't get it : what kind of corruption can fool the Scanner, but be transparent for the BufferedReader ? Anyway, Thanks again. My problem is solved. – Sharcoux Dec 02 '13 at 14:46
  • I'm also really not sure about that difference.Do you think the same file was given as input to BufferedReader code as well.The file was not properly encoded.that was the problem. Happy for you :) Can you please accept my answer?Happy coding! – Keerthivasan Dec 02 '13 at 14:53
  • No. The file was exactly the same. I'm sure. I was able to see all the lines being written while executing the code and I had only one copy of this file, so there couldn't be any confusion. – Sharcoux Dec 02 '13 at 19:15
  • scanner.next() is not reading the whole line, it just gets next word, nextLine() should be used instead. – megido May 24 '15 at 09:13
  • 1
    @megido You are right. but, this requirement has just one word per line. so, it worked. – Keerthivasan May 25 '15 at 04:18
  • `.next()` should be `.nextLine()` – ytpillai Mar 13 '16 at 22:53
6

next() and nextLine() methods are associated with Scanner and is used for getting String inputs. Their differences are...

next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.

nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

Read article :Difference between next() and nextLine()

Replace your while loop with :

while(r.hasNext()) {
                scan = r.next();
                System.out.println(scan);
                if(scan.length()==0) {continue;}
                //treatment
            }

Using hasNext() and next() methods will resolve the issue.

Nishant Lakhara
  • 1,943
  • 3
  • 21
  • 42
1
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.io.File;
import java.util.Scanner;

/**
 *
 * @author zsagga
 */
class openFile {
        private Scanner x ; 
        int count = 0 ; 
        String path = "C:\\Users\\zsagga\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\Readthis.txt"; 


    public void openFile() {
//                System.out.println("I'm Here");
        try {
            x = new Scanner(new File(path)); 

        } 
        catch (Exception e) {
            System.out.println("Could not find a file");
        }
    }

    public void readFile() {

        while (x.hasNextLine()){
            count ++ ;
            x.nextLine();     
        }
        System.out.println(count);
    }

    public void closeFile() {
        x.close();
    }
}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author zsagga
 */
public class JavaApplication1 {

    public static void main(String[] args) {
        // TODO code application logic here
        openFile r =  new openFile(); 
        r.openFile(); 
        r.readFile();
        r.closeFile(); 

    }
}
Zuhair Sagga
  • 301
  • 3
  • 4
1

For everybody who still can't read in a simple .txt file with the Java scanner.


I had the problem that the scanner couldn't read in the next line, when I Copy and Pasted the information, or when there was to much text in my file.

The solution is: Coder your .txt file into UTF-8.
This can be done fairly simple by saving opening the file again and changing the coding to UTF-8. (Under Win7 near the bottom right corner)

The Scanner shouldn't have any problem after this.

davejal
  • 5,431
  • 10
  • 33
  • 73
0

Try to use r.hasNext() instead of r.hasNextLine():

while(r.hasNext()) {
        scan = r.next();
yuvalhuck
  • 13
  • 4
  • r.hasNext() makes me enter the loop, but the problem is that r.nextLine() throws a java.util.NoSuchElementException: No line found – Sharcoux Dec 01 '13 at 11:06
  • Err, Sorry, actually, the r.hasNext() also returns false... I don't understand what's wrong – Sharcoux Dec 01 '13 at 13:11