java读写jar包自身包含的可执行文件
关键: byte[] data = new byte[in.available()];
import java.io.*;
public class ethTool {
public String saveFile() throws IOException {
File file = File.createTempFile("ethtool","");
file.setExecutable(true,false);//设置可执行权限
file.setReadable(true,false);//设置可读权限
file.setWritable(true,false);//设置可写权限
writeMethod(file,readMethod());
return file.getAbsolutePath();
}
public void writeMethod(File file, byte[] byteFile){
try{
DataOutputStream out=new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file)));
out.write(byteFile);
System.out.println(out.size()+" bytes have been written.");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] readMethod(){
try {
InputStream is = this.getClass().getResourceAsStream("/ethtool"); // windows可不加斜杠,linux必须加,否则获取不到
BufferedInputStream in = new BufferedInputStream(is);
try {
byte[] data = new byte[in.available()];
in.read(data);
return data;
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
ethTool et = new ethTool();
System.out.println(et.saveFile());
}
}