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

  • SEARCH
  • Home
  • 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 8993747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:15:49+00:00 2026-06-15T23:15:49+00:00

So, I just ran into an interesting problem while using the Scanner class to

  • 0

So, I just ran into an interesting problem while using the Scanner class to read contents from files. Basically, I’m trying to read several output files produced by a parsing application from a directory to compute some accuracy metrics.

Basically, my code just walks through each of the files in the directory, and opens them up with a scanner to process the contents. For whatever reason, a few of the files (all UTF-8 encoded) were not being read by the Scanner. Even though the files were not empty, scanner.hasNextLine() would return false upon its first call (I opened up the debugger and observed this). I was initializing the scanner directly with the File objects each time (the file Objects were successfully created). i.e:

    File file = new File(pathName);
    ...
    Scanner scanner = new Scanner(file);

I tried a couple of things, and was eventually able to fix this problem by initializing the scanner in the following way:

    Scanner scanner = new Scanner(new FileInputStream(file));

Though I’m happy to have solved the problem, I’m still curious as to what might have been happening to cause the problem before. Any ideas? Thanks so much!

  • 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-06-15T23:15:50+00:00Added an answer on June 15, 2026 at 11:15 pm

    According to the Scanner.java source in Java 6u23 a new line is detected by

    private static final String LINE_SEPARATOR_PATTERN = 
                                           "\r\n|[\n\r???]";
    private static final String LINE_PATTERN = ".*("+LINE_SEPARATOR_PATTERN+")|.+$";
    

    So you could check whether you can match the following regex to the content in the files that were not read.

    .*(\r\n|[\n\r???])|.+$
    

    Also I would check if there were some exception raised.

    UPDATE:
    This made me curious and I looked for answers. Seems your question has been asked and solved already here:
    Java Scanner(File) misbehaving, but Scanner(FIleInputStream) always works with the same file

    To summarize it’s about characters that are out of ASCII, that are behaving differently depending on whether you initialize your Scanner with File or FileInputStream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just ran into a problem.. I'm trying to build a website at the
Just ran into this very frustrating problem when trying to add a new view,
I've recently ran into a very interesting problem while writing a web app with
I just ran into a problem while developing a login system. I build a
I just ran into LINQPad today and was trying to add a connection using
I just ran into a problem involving Expressions . In my class<T> have a
I just ran into an issue while trying to write an bitmap-manipulating algo for
I just ran into this issue while making a GET request to a node.js
I just ran into a problem with the version of JAX-WS in JDK 1.6.0
I'm using ASP.NET MVC 3, and just ran into a 'gotcha' using the DropDownListFor

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.