我想创建可运行的jar文件但是在我的小项目中我有TextIO类用于在文件和文件.txt中读写。
就像这样 http://postimg.org/image/6lrcqnmil/
String fileName = "src/***/******/*******/mini/game/gameFile.txt";
String line = null;
String s = "";
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
s+=line;
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
/**TextIO.readFile("**/****/*****/gameFile.txt");
String s = "";
while(!TextIO.eof()){
s+=TextIO.getln();
s+="\n";
}**/
String fileName = "src/***/******/*******/mini/game/gameFile.txt";
String line = null;
String s = "";
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
s+=line;
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
JOptionPane.showMessageDialog(null,s);
首先,我尝试使用TextIO而不是使用此阅读器。
答案 0 :(得分:2)
我猜你的gameFile.txt文件在classpath中,所以你可以通过classloader访问它:
InputStream is = TextIO.class.getResourceAsStream("/gameFile.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
然后阅读内容......