如果它僅僅是數據,您將需要從資源讀取數據。
安排您的數據文件是在相同的位置類文件,並使用這樣的:
class Primes {
private static final ArrayList<Integer> NUMBERS = new ArrayList<>();
private static final String NUMBER_RESOURCE_NAME = "numbers.txt";
static {
try (InputStream in = Primes.class.getResourceAsStream(NUMBER_RESOURCE_NAME);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr)) {
for (String line; (line = br.readLine()) != null;) {
String[] numberStrings = line.split(",");
for (String numberString : numberStrings) {
if (numberString.trim().length() > 0) {
NUMBERS.add(Integer.valueOf(numberString));
}
}
}
} catch (NumberFormatException | IOException e) {
throw new IllegalStateException("Loading of static numbers failed", e);
}
}
}
我用它來閱讀1000張素數的逗號分隔的列表。
如果我們能看到有問題的代碼,我們將能夠幫助更多。請問 –
[SSCCE](http://www.sscce.org/)? – Xynariz
看看編譯器選項或拆分更多類 – lordkain