2009-01-21 292 views

回答

9

假設說罐子是在classpath:

URL url = getClassLoader().getResource("com/test/io/test.txt"); 
FileOutputStream output = new FileOutputStream("test.txt"); 
InputStream input = url.openStream(); 
byte [] buffer = new byte[4096]; 
int bytesRead = input.read(buffer); 
while (bytesRead != -1) { 
    output.write(buffer, 0, bytesRead); 
    bytesRead = input.read(buffer); 
} 
output.close(); 
input.close(); 
+0

你可以只使用.getResourceAsStream() :) – Cogsy 2009-01-21 03:33:33