Results 1 to 7 of 7
  1. #1
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default java.util.Scanner runs in Vista but not in Linux

    Hi all

    Following code runs in eclipse on Vista but not in eclipse on Linux:

    Java Code:
    // Catch the players.
    File file = new File("players.txt");
    try {        	
        // Create a new Scanner object
        Scanner scanner = new Scanner(file);
        int i = 0;
        // Check if there are more lines
        while (scanner.hasNextLine()) {
        	// Read one player
            parNames[i] = scanner.nextLine();
            i += 1;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    The file "players.txt" exist and this code gives no exceptions. If bad permissions I would expect an exception. The files permissions are "-rwxrwxrwx 1 root root 420 ... players.txt". parNames are filled with "null".

    Do anybody know what I can do?
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Are you sure you are actually finding the file "players.txt"? It seems it would look for it in the home directory of your current user in linux.

    Try this:

    Java Code:
    File file = new File("players.txt");
    System.out.println(file.exists());

    You can see where it is actually looking with this:

    Java Code:
    File file = new File(".");
    System.out.println(file.getAbsolutePath());

    And if you have the file in the same directory as the compiled class,
    Then try this:

    Java Code:
    File file = new File(this.getClass().getResource("players.txt").getFile());
    System.out.println(file.exists());

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Paths can make lots of trouble in Linux and Windows. Better to check all the file path as mrmatt1111 explain in the above code. Always hang with absolute path.

  4. #4
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Thank you for your answers. I have modified the code to contain the tests for file exists:

    Java Code:
    // Catch the players.
    File file = new File("players.txt");
    System.out.println("===========================");
    System.out.println(file.exists());
    System.out.println(file.getAbsolutePath());
    
    File file2 = new File(this.getClass().getResource("players.txt").getFile());
    System.out.println(file2.exists());
    System.out.println("===========================");
    try {            
        // Create a new Scanner object
        Scanner scanner = new Scanner(file);
        int i = 0;
        // Check if there are more lines
        System.out.println(scanner.hasNextLine());
        while (scanner.hasNextLine()) {
            // Read one player
            parNames[i] = scanner.nextLine();
            System.out.println(parNames[i]);                
            i += 1;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println("===========================");
    The output is

    Java Code:
    ===========================
    true
    /mnt/home/usr/workspace/BC_5/players.txt
    true
    ===========================
    false
    ===========================
    The last "false" is for scanner.hasNextLine so I have to correct my first statement that "parNames are filled with "null"" ... parNames are not touched. "players.txt" has 30 lines ... so why?
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    It seems to work for me (i'm running linux).

    I would suggest recreating the "players.txt" from scratch on your linux machine (don't copy and paste) because there may be bad formatting in the file. That is the only thing i can think of.

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Yes, the problem was in the text file. I use some danish letters in the lines of players.txt and that is unwelcome on the linux side.

    It is importent for me to be able to copy in Vista and paste in Linux. My Aspire One keyboard is english so I have to do some convertion programming.

    Thank you for all help.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. 'java is not recognized' vista
    By dylan mc in forum New To Java
    Replies: 3
    Last Post: 08-16-2009, 05:54 PM
  2. java.util.Scanner not recognised
    By pjm35@st-and.ac.uk in forum New To Java
    Replies: 4
    Last Post: 06-07-2008, 03:32 PM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Using ava.util.Scanner
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-20-2007, 04:47 PM
  5. The import java.util.Scanner cannot be resolved
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-08-2007, 01:05 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
  •