Hi,
Has anyone used
Java I/O classes to copy one JAR file to another? If so could you please let me know what is wrong with the following code? The following code runs without error and copies the source jar file to the destination with "zero" byte size.
If you have a better solution to copy JAR file at the runtime, please let me know.
Code starts:
-----------------
<Pre>
import java.io.*;
import java.util.jar.*;
public class CopyJarFile {
public static void main(
String args[]) {
String newLine = System.getProperty("line.separator");
FileInputStream fis = null;
FileOutputStream fos = null;
JarInputStream jis = null;
JarOutputStream jos = null;
BufferedReader br = null;
BufferedWriter bw = null;
File source = null;
if (args.length < 2)
System.out.println("Command Syntax: CopyFile <source>
<dest>\n\n<source>\tThe source file name (path optional)\n<dest>\t\tThe
destination file name (path optional)");
else
{
try
{
fis = new FileInputStream(args[0]);
fos = new FileOutputStream(args[1]);
jis = new JarInputStream(fis);
jos = new JarOutputStream(fos);
br = new BufferedReader(new InputStreamReader(jis));
bw = new BufferedWriter(new OutputStreamWriter(jos));
/* Determine the size of the buffer to allocate */
source = new File(args[0]);
int fileLength = (int) source.length();
char charBuff[] = new char[fileLength];
while (br.read(charBuff,0,fileLength) != -1)
bw.write(charBuff,0,fileLength);
}
catch(FileNotFoundException fnfe)
{System.out.println(args[0] + " does not exist!");}
catch(IOException ioe)
{System.out.println("Error reading/writing files!");}
finally
{
try
{
if (br != null)
br.close();
if (bw != null)
bw.close();
}
catch(IOException ioe){}
}
}
}
}
</Pre>
----------------
Code ends:
Thanks,
Padhy