io文件流关闭之前要刷新缓存流

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:29   2843   0
lose()
关闭此流,但要先刷新它。
flush()
刷新该流的缓冲。


/**
* 保存文件
* @param file 文件属性
* @param filePath 新的文件路径
*/
public static void saveFile(File file,String filePath) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(file); //根据上传文件得到输入流
os = new FileOutputStream(new File(filePath)); //指定文件输出流
byte buffer[] = new byte[4096];
int count = 0;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
os.flush();
}finally{
try {
os.close();
is.close();
} catch (IOException e) {
logger.debug(e);
throw new IOException("some unknown error happened! ");
}
}
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP