Java IO

大神指导的我!

之前的代码:

InputStream in = part.getInputStream();// 打开附件的输入流
java.io.FileOutputStream out = new FileOutputStream(strFileNmae);
int data;
while ((data = in.read()) != -1) {
	out.write(data);
}
in.close();
out.close();

改之后:
 

try {
         java.io.FileOutputStream out = new FileOutputStream(strFileNmae);
         try {
                  int data;
                  while ((data = in.read()) != -1) {
                       out.write(data);
                  }
                  } finally {
                       out.close();
                  }
             }finally {
                   in.close();
         }
}

因为IO的要占用资源,所以一定要确保最后关闭IO。

你可能感兴趣的:(Java,SE,java)