方式一:直接输入
@Testpublic void test2() throws IOException {
FileInputStream fileInputStream = new FileInputStream("我是存在的图.png");FileOutputStream fileOutputStream = new FileOutputStream("我是新建的空的图.png");int len = 0;while ((len = fileInputStream.read()) != -1) {
fileOutputStream.write(len);}fileInputStream.close();fileOutputStream.close();}
方式一:直接输入(try-catch型)
@Testpublic void test2() {
FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {
fileInputStream = new FileInputStream("我是存在的图.png");fileOutputStream = new FileOutputStream("我是新建的空的图.png");int len = 0;while ((len = fileInputStream.read()) != -1) {
fileOutputStream.write(len);}} catch (IOException e) {
e.printStackTrace();} finally {
if (fileInputStream != null && fileOutputStream != null){
try {
fileInputStream.close();fileOutputStream.close();} catch (IOException e) {
e.printStackTrace();}}}}
方式二:存入byte(try-catch型)
@Testpublic void test1(){
FileInputStream fileInputStream = null;ArrayList arrayList = new ArrayList();try {
File file = new File("QQ截图20200922195443.png");fileInputStream = new FileInputStream(file);int read;while ((read = fileInputStream.read()) != -1){
arrayList.add(read);}} catch (IOException e) {
e.printStackTrace();} finally {
if (fileInputStream != null){
try {
fileInputStream.close();} catch (IOException e) {
e.printStackTrace();}}}FileOutputStream fileOutputStream = null;try {
File file = new File("fuzhi.png");fileOutputStream = new FileOutputStream(file);Object[] array = arrayList.toArray();int i=0;byte[] bytes = new byte[array.length];while (i < array.length){
for (Object obj : array){
bytes[i] = (byte)Integer.parseInt(String.valueOf(obj));i++;}}fileOutputStream.write(bytes);} catch (IOException e) {
e.printStackTrace();} finally {
if (fileOutputStream != null){
try {
fileOutputStream.close();} catch (IOException e) {
e.printStackTrace();}}}}