1

我正在读取以字节为单位的视频文件数据并发送到另一个文件,但接收到的视频文件无法正常播放并且出现抖动。

谁能解释我为什么会发生这种情况并感谢解决方案。

我的代码如下

import java.io.*;

public class convert {

  public static void main(String[] args) {

    //create file object
    File file = new File("B:/music/Billa.mp4");

    try
    {
      //create FileInputStream object
      FileInputStream fin = new FileInputStream(file);


       byte fileContent[] = new byte[(int)file.length()];
       fin.read(fileContent);

       //create string from byte array
       String strFileContent = new String(fileContent);

       System.out.println("File content : ");
       System.out.println(strFileContent);

       File dest=new File("B://music//a.mp4");
       BufferedWriter bw=new BufferedWriter(new FileWriter(dest));
       bw.write(strFileContent+"\n");
       bw.flush();

    }
    catch(FileNotFoundException e)
    {
      System.out.println("File not found" + e);
    }
    catch(IOException ioe)
    {
      System.out.println("Exception while reading the file " + ioe);
    }
  }
}
4

4 回答 4

4

这个问题可能已经死了,但有人可能会觉得这很有用。

您不能将视频作为字符串处理。这是使用 Java 7 或更高版本读取和写入(复制)任何文件的正确方法。

请注意,缓冲区的大小取决于处理器,通常应该是 2 的幂。有关更多详细信息,请参阅此答案

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy {
public static void main(String args[]) {
    
    final int BUFFERSIZE = 4 * 1024;
    String sourceFilePath = "D:\\MyFolder\\MyVideo.avi";
    String outputFilePath = "D:\\OtherFolder\\MyVideo.avi";

    try(
            FileInputStream fin = new FileInputStream(new File(sourceFilePath));
            FileOutputStream fout = new FileOutputStream(new File(outputFilePath));
            ){
        
        byte[] buffer = new byte[BUFFERSIZE];
        
        while(fin.available() != 0) {
            bytesRead = fin.read(buffer);
            fout.write(buffer, 0, bytesRead);
        }
        
    }
    catch(Exception e) {
        System.out.println("Something went wrong! Reason: " + e.getMessage());
    }

    }
}
于 2017-10-17T14:11:25.893 回答
0
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;

import javax.imageio.ImageIO;

public class Reader {

    public Reader() throws Exception{


        File file = new File("C:/Users/Digilog/Downloads/Test.mp4");

        FileInputStream fin = new FileInputStream(file);
        byte b[] = new byte[(int)file.length()];
        fin.read(b);

        File nf = new File("D:/K.mp4");
        FileOutputStream fw = new FileOutputStream(nf);
        fw.write(b);
        fw.flush();
        fw.close();

    }

}
于 2015-03-17T08:37:23.297 回答
0

In addition to Jakub Orsula's answer, one needs to check the result of read operation to prevent garbage being written to end of file in last iteration.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy {
public static void main(String args[]) {

    final int BUFFERSIZE = 4 * 1024;
    String sourceFilePath = "D:\\MyFolder\\MyVideo.avi";
    String outputFilePath = "D:\\OtherFolder\\MyVideo.avi";

    try(
            FileInputStream fin = new FileInputStream(new File(sourceFilePath));
            FileOutputStream fout = new FileOutputStream(new File(outputFilePath));
            ){

        byte[] buffer = new byte[BUFFERSIZE];
        int bytesRead;

        while(fin.available() != 0) {
        bytesRead = fin.read(buffer);
        fout.write(buffer, 0, bytesRead);
        }

    }
    catch(Exception e) {
        System.out.println("Something went wrong! Reason: " + e.getMessage());
    }

    }
}
于 2018-04-14T16:48:28.800 回答
0

希望这对您也有帮助 - 这可以读取文件并将其写入另一个文件(您可以使用任何文件类型来执行此操作)

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Copy {
    public static void main(String[] args) throws Exception {
        FileInputStream input = new FileInputStream("input.mp4");      //input file
        byte[] data = input.readAllBytes();
        FileOutputStream output = new FileOutputStream("output.mp4");  //output file
        output.write(data);
        output.close();
    }
}
于 2021-01-22T08:43:46.443 回答