2013-04-19 97 views
0

我正在以字節爲單位讀取視頻文件數據併發送到另一個文件,但收到的視頻文件播放不正常並且發生了聊天。讀取視頻數據並寫入另一個文件java

任何人都可以解釋爲什麼發生這種情況,並感謝解決方案。

我的代碼如下

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); 
    } 
    } 
} 
+2

1)這與流媒體視頻有什麼關係?視頻的來源是一個文件! 2)**您不能將視頻數據視爲字符串或文本!它不是。** –

+0

'new byte [(int)file.length()]'可能會截斷文件中的字節,因爲'int'小於'long'。您需要將文件複製到chunk中 – MadProgrammer

+0

感謝您的回覆。我想加密數據,以便將文件讀入字符串。 – Krish

回答

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

由於其他原因,我使用了一些頭文件。不要爲此煩擾 –

+0

任何意見來解釋你的解決方案? –

0

這個問題可能是死的,但有人可能會發現這很有用。

您無法將視頻作爲字符串處理。這是使用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()); 
    } 

    } 
} 
相關問題