Java 使用BufferedInput/OutputStream进行文件管理
我使用BufferedInputStream和BufferedOutput Stream以编程方式复制mp4视频。它被正确地复制了,除了因为我丢失了音频!这是我的代码:Java 使用BufferedInput/OutputStream进行文件管理,java,file,Java,File,我使用BufferedInputStream和BufferedOutput Stream以编程方式复制mp4视频。它被正确地复制了,除了因为我丢失了音频!这是我的代码: FileInputStream fi = null; FileOutputStream fo = null; try { File f = new File("C:\\Users\\Zerok\\Videos\\v.mp4"); fi = new FileInputStre
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);
}
}
video.mp4文件工作正常,只是缺少音频。这是什么原因造成的?我是否正确使用了这些类?问题是,我没有关闭我的BufferedOutputStream。我使用了“资源试用”块,该块在资源完成后自动关闭资源:
try (BufferedOutputStream buffOut = new BufferedOutputStream(fo)) {
int read;
System.out.println("Beginning to copy...");
while((read = buffIn.read()) != -1){
buffOut.write(read);
}
}
现在音频可以完美再现。您没有关闭buffOut/fo。那是故意的吗?这是我唯一能说的。你失去音频的确切含义是什么?你刚刚获奖!这就是问题所在。