基于java的面向文件操作
我正试图通过以下代码将特定.mp4(视频)文件的“内容”复制到另一个文件中:基于java的面向文件操作,java,file,Java,File,我正试图通过以下代码将特定.mp4(视频)文件的“内容”复制到另一个文件中: Path source = Paths.get("E:\\Video0001.mp4"); Path destination = Paths.get("C:\\Ritesh\\Experimentation\\GOT.mp4"); Set<OpenOption> options = new HashSet<>(); options.add(APPEND); options.add(CREAT
Path source = Paths.get("E:\\Video0001.mp4");
Path destination = Paths.get("C:\\Ritesh\\Experimentation\\GOT.mp4");
Set<OpenOption> options = new HashSet<>();
options.add(APPEND);
options.add(CREATE);
try (SeekableByteChannel sbc = Files.newByteChannel(source);
SeekableByteChannel sbcdes = Files.newByteChannel(destination, options)) {
ByteBuffer buf = ByteBuffer.allocate(10);
String encoding = System.getProperty("source.encoding");
while (sbc.read(buf) > 0) {
buf.rewind();
ByteBuffer bb =
ByteBuffer.wrap(((Charset.forName(encoding).decode(buf)).toString()).getBytes());
sbcdes.write(bb);
buf.flip();
}
} catch (IOException x) {
System.out.println("caught exception: " + x);
}
Path source=Path.get(“E:\\Video0001.mp4”);
Path destination=Path.get(“C:\\Ritesh\\Experimentation\\get.mp4”);
Set options=new HashSet();
选项。添加(追加);
选项。添加(创建);
try(seekableytechnel sbc=Files.newbytechnel(源代码);
seekablybytechnel sbcdes=Files.newbytechnel(目标,选项)){
ByteBuffer buf=ByteBuffer.allocate(10);
字符串编码=System.getProperty(“source.encoding”);
而(sbc.read(buf)>0){
buf.倒带();
ByteBuffer bb=
wrap(((Charset.forName(encoding.decode(buf)).toString()).getBytes());
sbcdes.write(bb);
buf.flip();
}
}捕获(IOX异常){
System.out.println(“捕获的异常:+x);
}
代码执行时,在所需位置创建了一个目标.mp4文件,但问题是.mp4文件无法运行…它给我一条错误消息,说明无法识别输入的格式。。。文件操作对我来说还是个新鲜事物,使我无法解决这个难题。。。。
任何形式的帮助都将不胜感激 问题在于您正在对数据进行解码和编码(将其转换为
String
s并返回)。这会弄坏任何不是文本的东西。MP4文件不是文本
这应该是可行的,而不是您拥有的循环:
while (sbc.read(buf) > 0) {
buf.flip()
sbcdes.write(buf);
buf.clear();
}
也许我完全误解了你的问题,但是简单的复制和粘贴有什么错呢?您说destination.mp4是在所需的位置创建的,因此我认为您不想添加到目标文件中,而只是在目标上有一个源文件的克隆?您正在查找
文件.copy(源,目标)
?我很了解并精通Files.copy方法……我只是想更仔细地看看它的复杂工作原理