I have a file in a JAR file. It's 1.txt
, for example.
How can I access it? My source code is:
Double result=0.0; File file = new File("1.txt")); //how get this file from a jar file BufferedReader input = new BufferedReader(new FileReader(file)); String line; while ((line = input.readLine()) != null) { if(me==Integer.parseInt(line.split(":")[0])){ result= parseDouble(line.split(":")[1]); } } input.close(); return result;
To unpackage a JAR, you need a program that can extract compressed files. Windows includes functionality for this, but you can also use file extraction software like 7-Zip or WinRAR to get the job done. Open the JAR file within the software, and you can browse all the folders and files within it.
Select “File,” then click “Open Archive” to open your JAR file, or click the “Open” icon. Browse to find the file's location, select it, and then click “Open.”
If your jar is on the classpath:
InputStream is = YourClass.class.getResourceAsStream("1.txt");
If it is not on the classpath, then you can access it via:
URL url = new URL("jar:file:/absolute/location/of/yourJar.jar!/1.txt"); InputStream is = url.openStream();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With