Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Scanner.java#Scanner.hasNextLine%28%29" rel="noreferrer">hasNextLine()</a> calls <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Scanner.java#Scanner.findWithinHorizon%28java.util.regex.Pattern,int%29" rel="noreferrer">findWithinHorizon()</a> which in turns calls <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Scanner.java#Scanner.findPatternInBuffer%28java.util.regex.Pattern,int%29" rel="noreferrer">findPatternInBuffer()</a>, searching a match for a line terminator character pattern defined as <code>.*(\r\n|[\n\r\u2028\u2029\u0085])|.+$</code></p> <p>Strange thing is that with both ways to construct a Scanner (with FileInputStream or via File), findPatternInBuffer returns a positive match if the file contains (independently from file size) for instance the 0x0A line terminator; but in the case the file contains a character out of ascii (ie >= 7f), using FileInputStream returns true while using File returns false.</p> <p>Very simple test case:</p> <p>create a file which contains just char "a"</p> <pre><code># hexedit file 00000000 61 0A a. # java Test.java using File: true using FileInputStream: true </code></pre> <p>now edit the file with hexedit to:</p> <pre><code># hexedit file 00000000 61 0A 80 a.. # java Test.java using File: false using FileInputStream: true </code></pre> <p>in the test java code there is nothing else than what already in the question:</p> <pre><code>import java.io.*; import java.lang.*; import java.util.*; public class Test { public static void main(String[] args) { try { File file1 = new File("file"); Scanner s1 = new Scanner(file1); System.out.println("using File: "+s1.hasNextLine()); File file2 = new File("file"); Scanner s2 = new Scanner(new FileInputStream(file2)); System.out.println("using FileInputStream: "+s2.hasNextLine()); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>SO, it turns out this is a charset issue. In facts, changing the test to:</p> <pre><code> Scanner s1 = new Scanner(file1, "latin1"); </code></pre> <p>we get:</p> <pre><code># java Test using File: true using FileInputStream: true </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload