Could someone tell me how I can modify text files that are found in a jar, everything from java code. I've tried everything but nothing works.
Could someone tell me how I can modify text files that are found in a jar, everything from java code. I've tried everything but nothing works.
Because a .jar file is considered a compressed file similar to a .zip, you can use the zip
package provided by Java:
Provides classes to read and write the standard ZIP and GZIP file formats.
This example contains two methods:
fichero.jar
% of the file archivo.txt
) Read (Read the name of the files within fichero.jar
)
public class Clase {
public static void main(String[] args) throws FileNotFoundException, IOException {
Escribir();
Leer();
}
public static void Escribir() throws IOException {
ZipOutputStream os = new ZipOutputStream(new FileOutputStream("fichero.jar"));
os.setLevel(Deflater.DEFAULT_COMPRESSION);
os.setMethod(Deflater.DEFLATED);
ZipEntry entrada = new ZipEntry("archivo.txt");
os.putNextEntry(entrada);
FileInputStream fis = new FileInputStream("archivo.txt");
byte[] buffer = new byte[1024];
int leido = 0;
while (0 < (leido = fis.read(buffer))) {
os.write(buffer, 0, leido);
}
fis.close();
os.closeEntry();
os.close();
}
public static void Leer() throws IOException {
@SuppressWarnings("resource")
ZipInputStream zis = new ZipInputStream(new FileInputStream("fichero.jar"));
ZipEntry entrada;
while (null != (entrada = zis.getNextEntry())) {
System.out.println(entrada.getName());
FileOutputStream fos = new FileOutputStream(entrada.getName());
int leido;
byte[] buffer = new byte[1024];
while (0 < (leido = zis.read(buffer))) {
fos.write(buffer, 0, leido);
}
fos.close();
zis.closeEntry();
}
}
}
With this class, you may be able to read and modify the .txt files you need. Feel free to make the adjustments you consider relevant.