2012-08-16 144 views
1

我看过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中的文件夹和文件?

感谢

+1

如何在jar中搜索时使用/而不是\\? – 2012-08-16 15:04:20

+0

尝试过,同样的事 – 2012-08-16 15:04:41

+0

您是否知道您正在指定与CT类所在的包相关的in-jar路径?最好使用'getResource(“/ res/CDC.txt”)路径' – Robert 2012-08-16 15:17:56

回答

0

你将要使用getSystemResourceAsStream()来读取文件的内容,在一个罐子里。

这当然假设该文件实际上是在其他用户计算机的类路径上。

相关问题