public static void main(String[] args) {
try {
// setImgByte();
resetImg();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void setImgByte() throws FileNotFoundException {
File file = new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green.png");
FileInputStream inputStream = new FileInputStream(file);
String flag = "我是一张图片";
FileOutputStream outputStream = new FileOutputStream(new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green2.png"));
try {
byte[] cs = flag.getBytes("utf-8");
byte[] bytes = IOUtils.toByteArray(inputStream);
byte[] tbytes = new byte[bytes.length + cs.length];
System.arraycopy(cs, 0, tbytes, 0, cs.length);
System.arraycopy(bytes, 0, tbytes, cs.length, bytes.length);
IOUtils.write(tbytes, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void resetImg() throws FileNotFoundException {
File file = new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green2.png");
FileInputStream inputStream = new FileInputStream(file);
try {
byte[] bytes = IOUtils.toByteArray(inputStream);
byte[] flag = new byte[18];
System.arraycopy(bytes, 0, flag, 0, 18);
String s = new String(flag, "UTF-8");
if (s.equals("我是一张图片")) {
FileOutputStream outputStream = new FileOutputStream(new File("d:\\user\\01411436\\桌面\\tool\\cabinet_green3.png"));
byte[] tbytes = new byte[bytes.length - flag.length];
System.arraycopy(bytes, 18, tbytes, 0, tbytes.length);
IOUtils.write(tbytes, outputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
}