Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:20:20+00:00 2026-05-31T00:20:20+00:00

I am having weird behavior with Scanner. It will work with a particular set

  • 0

I am having weird behavior with Scanner. It will work with a particular set of files I am using when I use the Scanner(FileInputStream) constructor, but it won’t with the Scanner(File) constructor.

Case 1: Scanner(File)

Scanner s = new Scanner(new File("file"));
while(s.hasNextLine()) {
    System.out.println(s.nextLine());
}

Result: no output

Case 2: Scanner(FileInputStream)

Scanner s = new Scanner(new FileInputStream(new File("file")));
while(s.hasNextLine()) {
    System.out.println(s.nextLine());
}

Result: the file content outputs to the console.

The input file is a java file containing a single class.

I double checked programmatically (in Java) that:

  • the file exists,
  • is readable,
  • and has a non-zero filesize.

Typically Scanner(File) works for me in this case, I am not sure why it doesn’t now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:20:22+00:00Added an answer on May 31, 2026 at 12:20 am

    hasNextLine() calls findWithinHorizon() which in turns calls findPatternInBuffer(), searching a match for a line terminator character pattern defined as .*(\r\n|[\n\r\u2028\u2029\u0085])|.+$

    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.

    Very simple test case:

    create a file which contains just char “a”

    # hexedit file     
    00000000   61 0A                                                a.
    
    # java Test.java
    using File: true
    using FileInputStream: true
    

    now edit the file with hexedit to:

    # hexedit file
    00000000   61 0A 80                                             a..
    
    # java Test.java
    using File: false
    using FileInputStream: true
    

    in the test java code there is nothing else than what already in the question:

    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();
            }
        }
    }
    

    SO, it turns out this is a charset issue. In facts, changing the test to:

     Scanner s1 = new Scanner(file1, "latin1");
    

    we get:

    # java Test 
    using File: true
    using FileInputStream: true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just getting into ajax using MVC3. I am having weird behavior with IE8. The
I'm having a bit of weird behavior that I can't seem to work out.
We're having this weird behavior regarding our logs. We're using log4net and here is
I am using SQL Server 2005, and I found weird behavior of the HAVING
I'm having a weird behavior with IE: After some modifications on a Javascript enabled
I have InApp purchasing setup in my app. I am having some weird behavior
Having a weird problem here. Everybody knows that if you use web.config's customErrors section
I am trying to write the manifest file but I am having weird problem
I was having some weird Glib behavior, searched the internet a little and found
I was hoping to use CKeditor with GeSHi, but I'm having some major difficulties.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.