try catch语句 和 定义FileOutputStream时 报错

a271256939 2017-06-04 05:52:18
确切我也不知道具体问题是啥 直接看现象
try{fileInputStream=new FileInputStream(pic);
byte [] bufstring=new byte[1024];
FileOutputStream fileOutputStream=new FileOutputStream("C:/Users/wowowo/Desktop/ww.png");
后边没写,fileOuputStream是有警告的,而 fileInpuStream是没有警告的,同样在try外面先定义一下fileOutStream警告消失 为什么会这样?
...全文
407 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
OPPPPOP 2017-06-07
  • 打赏
  • 举报
回复
暗色三角是没保存 流接口要求使用完必须得关闭 通常放到finally里处理
try {
			FileInputStream in = new FileInputStream("xxx");
			try {
				FileOutputStream out = new FileOutputStream("xxx");
				try {
					// do xxx
				} finally {
					out.close();
				}
			} finally {
				in.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
Mcmurphyed 2017-06-07
  • 打赏
  • 举报
回复
InputStream是个抽象类 由它的子类FileInputStream实现
a271256939 2017-06-07
  • 打赏
  • 举报
回复
引用 1 楼 hanpoyangtitan 的回复:
流最后是要关闭的,不论是正常的情况还是异常的情况,一旦打开就需要最后关闭,所以一般是写道finally里

InputStream in;
try{
   in = new FileInputStream("ip.dat");
   .....
} catch(IOException | FileNotFoundException ex) {
    logger.error("0x2AD001", ex);
} finally {
    if(null != in){
        try{
            in.close();
        }catch(IOException e){
            logger.error("0x2AD002", ex)
        }
    }
}
确实是因为没有关闭而导致,但是如果我先定义赋值为null 在new一个fileoutputstream那么eclipse的警告就会变成暗的,就是不在警告处画黄线,而是在最左边有暗色三角警告标志 提前定义声明为null与直接定义并初始化差别在哪
摇头晃奶 2017-06-05
  • 打赏
  • 举报
回复
改成 FileOutputStream fileOutputStream=new FileOutputStream(new File("C:/Users/wowowo/Desktop/ww.png"));
soton_dolphin 2017-06-05
  • 打赏
  • 举报
回复
没看懂你在说什么
什么都不能 2017-06-04
  • 打赏
  • 举报
回复
流最后是要关闭的,不论是正常的情况还是异常的情况,一旦打开就需要最后关闭,所以一般是写道finally里

InputStream in;
try{
   in = new FileInputStream("ip.dat");
   .....
} catch(IOException | FileNotFoundException ex) {
    logger.error("0x2AD001", ex);
} finally {
    if(null != in){
        try{
            in.close();
        }catch(IOException e){
            logger.error("0x2AD002", ex)
        }
    }
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧