Results 1 to 5 of 5
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Question Error Reading File

    I'm reading files using a scanner, like this:

    Java Code:
    Scanner fileReader = new Scanner(new File("fileName.txt"));
    However, whenever the first line is read, it reads some strange characters before interpreting what's actually written there, even though there clearly are no strange signs in the file. This only happens with the first line. Can you explain what I'm doing wrong?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Error Reading File

    it reads some strange characters
    Can you post something showing what you are talking about?
    What do you see when you open the file with a simple editor? Do you have a hex-editor? Use it to look at the file's contents.
    How was the file created?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Error Reading File

    It seems to always display these: "", directly followed by the word that's actually on that line.

    When I open the file I see none of those characters, I just see the words that are written there.

    A hex-editor? I'm using BlueJ to code and Notepad to store the words.

    It was created using Notepad.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Error Reading File

    Is the file in Unicode format vs ASCII? With a Unicode file on Windows, the first two bytes of the file contain: 0xFFFE. I don't know that you can create a Unicode file with notepad. You can create one with wordpad.
    Look at the file with a hex-editor and see what are in the first bytes of the file.
    Also compare the number of bytes the file takes on disk with the number of characters in the file. A Unicode file will take 2 bytes per character, an ASCII file takes 1 byte per character.
    Last edited by Norm; 12-02-2013 at 03:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Error Reading File

    Normally I don't push apps but I highly recommend Vim. It's been around for decades and is a more powerful version of vi which has been around even longer. It also allows you to view files in hex mode. A great development tool and it comes with excellent documentation.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Error reading file
    By KevinNguyen in forum New To Java
    Replies: 1
    Last Post: 11-10-2012, 04:29 PM
  2. Error in reading from a file containing integers
    By stud91 in forum Advanced Java
    Replies: 1
    Last Post: 07-07-2012, 05:24 PM
  3. Error reading file
    By sehudson in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 04:44 AM
  4. Reading A CSV file error.
    By jetnor in forum New To Java
    Replies: 3
    Last Post: 02-05-2011, 03:51 AM
  5. reading from a zip file, error
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2008, 12:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •