使用BufferedInput/OutputStream进行java文件管理
我使用BufferedInputStream和BufferedOutput Stream以编程方式复制mp4视频。它被正确复制了,只是因为我丢失了音频!这是我的代码:
FileInputStream fi = null;
FileOutputStream fo = null;
try {
File f = new File("C:\\Users\\Zerok\\Videos\\v.mp4");
fi = new FileInputStream(f);
fo = new FileOutputStream("video.mp4");
BufferedInputStream buffIn = new BufferedInputStream(fi);
BufferedOutputStream buffOut = new BufferedOutputStream(fo);
int read;
while((read = buffIn.read()) != -1){
buffOut.write(read);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication13.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JavaApplication13.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fi.close();
} catch (IOException ex) {
Logger.getLogger(JavaApplication13.class.getName()).log(Level.SEVERE, null, ex);
}
}
视频。mp4文件工作正常,只是音频不足。这是什么原因?我正确使用这些课程了吗
# 1 楼答案
问题是,我没有关闭缓冲输出流。我使用了“资源尝试”块,它会在资源完成后自动关闭资源:
现在,音频可以完美地再现