How to copy a text file from a jar to a file outside the jar?

Say I have a file called test.txt in the package "com.test.io" in my bank.

How can I write a class that extracts this text file and then copies the contents to a new file in the file system?

+3
java file io jar
source share
1 answer

Assuming the specified jar is in your 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(); 
+9
source share

All Articles