Jaswinder Singh
Jaswinder Singh

Reputation: 91

Java IO copy video file

class HelloWorld {
    public static void main(String args[]) {
        File file = new File("d://1.mp4");
        FileInputStream fr = null;
        FileOutputStream fw = null;
        byte a[] = new byte[(int) file.length()];
        try {
            fr = new FileInputStream(file);
            fw = new FileOutputStream("d://2.mp4");

            fr.read(a);
            fw.write(a);
            fw.write(a);
            fw.write(a);
            fw.write(a);
            fw.write(a);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fr.close();
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Here i write fw.write(a) five times, the size of the file increases to 5x but the original 1.mp4 and copy 2.mp4 both have same length i.e. 3:30 minutes ?

Upvotes: 0

Views: 703

Answers (2)

Kamal Chanda
Kamal Chanda

Reputation: 173

FileOutputStream fooStream = new FileOutputStream("FilePath", false);

This will overwrite the content and the size of the file created will be same size as of original file.

Upvotes: 0

Koekje
Koekje

Reputation: 777

Simply duplicating the bytes of certain files does not necessarily mean it simply duplicates things when inspecting them with software. For example, the video player might read the data until some terminal is encountered and not look forward. This terminal would then exist at the end of the first file data block.

You could open the new file with a hex editor and check if you can see the data of the original video file five times in a row.

Upvotes: 1

Related Questions