读取视频数据并写入另一个文件java

时间:2013-04-19 01:33:22

标签: java file file-io copy video-streaming

我正在以字节为单位读取视频文件数据并发送到另一个文件,但收到的视频文件无法正常播放并被喋喋不休。

任何人都可以向我解释为什么会发生这种情况并且感谢解决方案。

我的代码如下

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 个答案:

答案 0 :(得分:1)

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

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

请注意,缓冲区的大小取决于处理器,通常应为2的幂。有关详细信息,请参阅this answer

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) {
        fin.read(buffer);
        fout.write(buffer);
        }

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

    }
}

答案 1 :(得分: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();

    }

}

答案 2 :(得分:0)

除了Jakub Orsula's answer之外,还需要检查读取操作的结果,以防止在上一次迭代中将垃圾写入文件末尾。

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());
    }

    }
}

答案 3 :(得分: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();
    }
}