Suppose my entire Eclipse project is in a directory called project
and in project
, there is a regular non-empty, well-formatted file, data.txt
, and a single directory src
. In src
, there are two directories: code
and test
for packages named exactly the same as the directories.
In code
, I have the following file A.java
package code;
import java.util.*;
import java.io.*;
public class A {
private static Map<Integer, String> m = makeMap("data.txt");
private static Map<Integer, String> makeMap(String file) {
Map<Integer, String> m = new HashMap<Integer, String>();
try {
int i = 0;
Scanner s = new Scanner(new File(file));
while (s.hasNextLine()) {
m.put(i, s.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
return m;
}
// a bunch of public static methods that use the global static variable m
}
In test
, I have ATest.java
, which is a JUnit test file for A.java
. It just contains several test cases that test the public static functions in A.java
.
Also in test
, I have another file TestSuite.java
, which is the following
package test;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.junit.runner.RunWith;
@RunWith(Suite.class)
@SuiteClasses({ ATest.class })
public final class TestSuite { }
If I were to run ATest
on any machine, it will pass all the tests. If I were to run TestSuite
on a Windows 10 machine, the tests in ATest
will fail, but if I were run TestSuite
on a Fedora 23 Linux machine, TestSuite
passes all of the tests.
In particular, on Windows, the program is able to successfully make it all the way through the try
block without throwing an exception, but the problem is that s.hasNextLine()
returns false
so it never reads the contents of the file. This is contrary on a Fedora, where s.hasNextLine()
returns true
and proceeds to do be able to perform actions on the global map. The thing that confuses me is that the data.txt
is the same on both platforms, so why would one platform perform differently than the other?
Does anybody know why this is happening, and what I can do to make my code be platform independent?
EDITS:
data.txt
contains Japanese characters, so it is encoded in UTF-8 if that helps
You need to specify the encoding of the file that you are reading. If it is not specified, it will use the platform default which is not always UTF-8
This is done using the Scanner(File, String charset)
constructor.