我看過How to access resources in JAR file?和How do I copy a text file from a jar into a file outside of the jar?以及許多其他questiions,但無法真正得到答案。我想要做的是將文件的res/CDC.txt
中的內容複製到jar中的某處。現在,在我的電腦上它可以工作,但是當我在不同的計算機上嘗試時,我得到FileNotFoundException
。所以,我找出了它爲什麼對我有用。我有一個CLASSPATH設置爲.;D:\myname\Java\JavaFiles
所有我的java文件都位於包中。在「JavaFiles」目錄中還有「res/CDC.txt」。因此,當我啓動我的應用程序時,它首先檢查myapp.jar位於「res/CDC.txt」中的當前目錄,然後檢查「JavaFiles」並找到它。其他電腦沒有它。所以,這是我最初的代碼:如何訪問jar文件內的文件夾內的文件?
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中搜索時使用/而不是\\? – 2012-08-16 15:04:20
嘗試過,同樣的事 – 2012-08-16 15:04:41
您是否知道您正在指定與CT類所在的包相關的in-jar路徑?最好使用'getResource(「/ res/CDC.txt」)路徑' – Robert 2012-08-16 15:17:56