【发布时间】:2015-01-02 10:19:27
【问题描述】:
我查看了How to access resources in JAR file? 和How do I copy a text file from a jar into a file outside of the jar? 以及许多其他问题,但实际上无法得到答案。我正在尝试做的是将 jar 中 res/CDC.txt 中的文件内容复制到 jar 中的某个位置。现在,在我的计算机上它可以工作,但是当我在不同的计算机上尝试它时,我得到FileNotFoundException。所以,我弄清楚了为什么它对我有用。我有一个 CLASSPATH 设置为 .;D:\myname\Java\JavaFiles 我所有的 java 文件都位于包中。在“JavaFiles”目录中还有“res/CDC.txt”。因此,当我启动我的应用程序时,它首先检查 myapp.jar 所在的当前目录中的“res/CDC.txt”,然后检查“JavaFiles”并找到它。其他电脑没有。所以,这是我的初始代码:
public final class CT
{
//Other fields
private static CT ct;
private NTSystem nts;
private File f1;
private File f6;
private PrintWriter pw1;
private BufferedReader br1;
//Other fields
public static void main(String[] args)
{
try
{
showMessage("Executing program...");
ct = new CT();
ct.init();
ct.create();
ct.insertData();
//Other code
showMessage("Program executed!");
}
catch(Exception e)
{
showMessage("An exception occured! Program closed.");
e.printStackTrace();
System.exit(0);
}
}
private void init()
throws IOException
{
//Other initialization
nts = new NTSystem();
f1 = new File("C:\\Users\\" + nts.getName() + "\\blahblah");
f6 = new File("res\\CDC.txt");
br1 = new BufferedReader(new FileReader(f6));
//Other initialization
showMessage("Initialized");
}
private void create()
throws IOException
{
//Makes sure file/dir exists, etc
pw1 = new PrintWriter(new BufferedWriter(new FileWriter(f1)), true);
//Other Stuff
showMessage("Created");
}
private void insertData()
throws IOException
{
String line = br1.readLine();
while(line != null)
{
pw1.println(line);
line = br1.readLine();
}
//Other stuff
showMessage("Data inserted");
}
private static void showMessage(String msg)
{
System.out.println(msg);
}
}
我改成了
public final class CT
{
//Other fields
private static CT ct;
private NTSystem nts;
private byte[] buffer;
private File f1;
private URL url1;
private FileOutputStream fos1;
private InputStream is1;
//Other fields
public static void main(String[] args)
{
try
{
showMessage("Executing program...");
ct = new CT();
ct.init();
ct.create();
ct.insertData();
//Other code
showMessage("Program executed!");
}
catch(Exception e)
{
showMessage("An exception occured! Program closed.");
e.printStackTrace();
System.exit(0);
}
}
private void init()
throws IOException
{
//Other initialization
nts = new NTSystem();
buffer = new byte[4096];
f1 = new File("C:\\Users\\" + nts.getName() + "\\blahblah");
url1 = getClass().getClassLoader.getResource("res\\CDC.txt"); //Also tried url1 = ct.getClass().getClassLoader.getResource("res\\CDC.txt"); or url1 = this.getClass().getClassLoader.getResource("res\\CDC.txt"); or url1 = CT.getClass().getClassLoader.getResource("res\\CDC.txt");
is1 = url1.openStream();
//Other initialization
showMessage("Initialized");
}
private void create()
throws IOException
{
//Makes sure file/dir exists, etc
pw1 = new PrintWriter(new BufferedWriter(new FileWriter(f1)), true);
//Other Stuff
showMessage("Created");
}
private void insertData()
throws IOException
{
int read = is1.read(buffer);
while(line != null)
{
fos1.write(buffer, 0, read);
read = is1.read(buffer);
}
//Other stuff
showMessage("Data inserted");
}
private static void showMessage(String msg)
{
System.out.println(msg);
}
}
这一次我总是得到NullPointerException。那么,如何读取jar中的文件夹和文件呢?
谢谢
【问题讨论】:
-
在 jar 中搜索时使用 / 代替 \\ 怎么样?
-
您是否知道您正在指定相对于 CT 类所在的包的 in-jar 路径?最好使用路径
getResource("/res/CDC.txt") -
你需要 myapp.jar 在你的类路径中
-
@Robert 在这些例子中的哪一个?上例还是下例?请给我整条线好吗? Tnx!