run text file inside a jar
Hi all,
Is there a way to run a text file (in notepad) that is located inside a jar, zip or rar file. I can run it with notepad if it not compressed, by;
Runtime.getRuntime().exec("notepad a.txt");
But I couldn't do it when it is compressed.
All helps will be appreciated, thanks...
No, Notepad.exe doesn't know anything about JAR (or ZIP) file,
so you cannot tell it to read the input from a JAR (or ZIP) file.
No.
What you could do is use getResourceAsStream to read the text file out,
write it into a temporary file in a temporary directory,
then tell Notepad to read that file.
Thanks for your answer KathyMcDonnell,
I learned to read files with Scanner class and its method (next.line()) and am not familiar with Streams too much. Is it possible to get a File from that jar or rar instead of stream, or is there a way to use Scanner and Formatter to read and create that temporary file?
Regards...
Hi KathyMcDonnell,
Thanks for all your help InputStream solved everything as you mentioned, however I didn't use buffers instead as I guessed, Scanner and Formatter just worked fine. For people who are interested I am posting the code, because I think it is easier than buffers.
Hangman is my main class (and only one).
InputStream is = Hangman.class.getResourceAsStream("ReadMe.txt");
Scanner reader = new Scanner (is);
Formatter writer = null;
try {
writer = new Formatter (new File ("c:/ReadMe.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
while (reader.hasNext())
{
String line = reader.nextLine();
writer.format(line + "%n");
}
}catch (NoSuchElementException e){}
writer.close();
File readMe = new File("c:/ReadMe.txt");
Process p = null;
try {
p = Runtime.getRuntime().exec("notepad " + readMe);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (p.waitFor() == 0)
{
readMe.delete();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Regards..