我知道这个问题之前已被问过一百次,但我尝试过的任何东西似乎都没有用。
我只想将文本文件加载到JAR中。我尝试过的大多数方法都可以在Eclipse中正常工作,但一旦导出就不行。以下是我在下面尝试过的一些代码:
public String[] loadTranslations(String trans){
String name = "/translations/"+trans+".txt";
ArrayList<String> lines = new ArrayList<String>();
URL url = this.getClass().getResource(name);
System.out.println("URL: " + url);
File f = new File(url.getFile());
try {
FileReader reader = new FileReader(f);
try {
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException notFoundEx) {
System.out.println("Caught FileNotFoundException: " + notFoundEx.getMessage());
}
return lines.toArray(new String[0]);
}
和......
public String[] loadTranslations3(String trans){
String name = "translations/"+trans+".txt";
ArrayList<String> lines = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(name)));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lines.toArray(new String[0]);
}
该功能应该由
等命令运行String[] translations = loadTranslations("EN");
我尝试过使用/不使用斜杠等。
我在src目录中有我的翻译文件夹,它包含在构建路径中:
异常是一个NullPOinter:
当我尝试运行第二个代码示例时,这是转储:
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at com.animation.shop.Main.loadTranslations(Main.java:215)
at com.animation.shop.Main.initTranslations(Main.java:287)
at com.animation.shop.Main.initVars(Main.java:197)
at com.animation.shop.Main.<init>(Main.java:1142)
at com.animation.shop.Main$1.run(Main.java:361)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatc...
第215行:
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(name)));
答案 0 :(得分:0)
仍然不太确定那里存在什么问题,可能是我忽略了但我通过使用processing.core.PApplet loadStrings()函数来实现它: 代码:(basicPapplet扩展了processing.core.PApplet)
public String[] loadTranslations(String trans){
String name = "data/translations/"+trans+".txt";
basicPapplet bp = new basicPapplet();
返回bp.loadStrings(name);
}
答案 1 :(得分:0)
你的第二种方法工作正常。以下是我的工作方式:
public class Test {
public static void main(String[] args) {
Test test = new Test();
String[] strings = test.loadTranslations3("test");
for(String someString : strings) {
System.out.println(someString);
}
}
public String[] loadTranslations3(String trans) {
String name = "translations/" + trans + ".txt";
ArrayList<String> lines = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
getClass().getClassLoader().getResourceAsStream(name)));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lines.toArray(new String[0]);
}
}
我在eclipse中的文件夹结构是:
src/somepackage/Test.java
src/translations/test.txt
从Eclipse导出时,导出为Runnable JAR文件,并指向Test.java类。
另外,请确保在项目属性中,src文件夹具有:
Included: (All)
Excluded:(None)