io流read的try-catch问题



import org.junit.Test;

import java.io.*;


public class PicTest {
    //图片的加密
    @Test
    public void test1(){
        FileInputStream fis= null;
        FileOutputStream fos= null;
        try {
           fis=new FileInputStream("Screen Shot 2021-02-01 at 10.21.31 PM.png");
           fos=new FileOutputStream("secret.png");
            byte[] buffer=new byte[20];
            int len;
            while((len=fis.read(buffer))!=-1){
                for (int i = 0; i < len; i++) {
                    buffer[i]=(byte) (buffer[i]^5);
                }
                fos.write(buffer,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos!=null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fis!=null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

/PicTest.java:26:32
java: unreported exception java.io.IOException; must be caught or declared to be thrown

这个fis.read 和fos.write已经try-catch了但是为什么还是报错,

你这finally里还嵌套try catch干嘛,建议直接在方法上thrown Exception

 

可能是catch没补到吧,补个Excep tion

因为read抛出的是ioexception 你捕获的是文件存不存在

代码替换 

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PicTest  {
    //图片的加密
    @Test
    public void test1() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("Screen Shot 2021-02-01 at 10.21.31 PM.png");
            fos = new FileOutputStream("secret.png");
            byte[] buffer = new byte[20];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (fis != null)
                    fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

楼上同学的代码还可以精简一点,使用jdk1.7的try-resource

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PicTest {
    //图片的加密
    @Test
    public void test1() {
        try (FileInputStream fis = new FileInputStream("Screen Shot 2021-02-01 at 10.21.31 PM.png");
             FileOutputStream fos = new FileOutputStream("secret.png")) {
            byte[] buffer = new byte[20];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}