1

I am reading a video file data in bytes and sending to another file but the received video file is not playing properly and is chattered.

Can anyone explain me why this is happening and a solution is appreciated.

My code is as follows

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);
    }
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Krish
  • 308
  • 3
  • 5
  • 19

4 Answers4

5

This question might be dead but someone might find this useful.

You can't handle video as string. This is the correct way to read and write (copy) any file using Java 7 or higher.

Please note that size of buffer is processor-dependent and usually should be a power of 2. See this answer for more details.

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

    }
}
Jakub Orsula
  • 75
  • 2
  • 10
2

Hope this also helpful for you - This can read and write a file into another file (You can use any file type to do that)

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();
    }
}
  • 1
    Hi Suchin and welcome to StackOverflow. Since the question is very old and appears to have already been thoroughly answered, you should explain how your solution improves on the previous ones. As a general rule, please avoid pasting code without explanation. – MathMax Jan 22 '21 at 09:25
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();

    }

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

    }
}