Results 1 to 7 of 7
- 08-22-2009, 07:35 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 54
- Rep Power
- 0
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:
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".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(); }
Do anybody know what I can do?-
Life is not the worst thing we have ... in a few minutes my coffee is ready.
- 08-22-2009, 11:20 PM #2
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());
- 08-23-2009, 03:40 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 08-23-2009, 06:03 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 54
- Rep Power
- 0
Thank you for your answers. I have modified the code to contain the tests for file exists:
The output isJava 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 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?Java Code:=========================== true /mnt/home/usr/workspace/BC_5/players.txt true =========================== false ===========================
-
Life is not the worst thing we have ... in a few minutes my coffee is ready.
- 08-23-2009, 06:58 PM #5
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.
- 08-23-2009, 08:25 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 54
- Rep Power
- 0
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.
- 08-24-2009, 03:50 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you've solve the problem please mark the thread solved from the tools menu on top of the thread.
Similar Threads
-
'java is not recognized' vista
By dylan mc in forum New To JavaReplies: 3Last Post: 08-16-2009, 05:54 PM -
java.util.Scanner not recognised
By pjm35@st-and.ac.uk in forum New To JavaReplies: 4Last Post: 06-07-2008, 03:32 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
Using ava.util.Scanner
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:47 PM -
The import java.util.Scanner cannot be resolved
By Heather in forum Advanced JavaReplies: 1Last Post: 07-08-2007, 01:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks