2011-09-02 139 views
2

是否可以將音頻mp3文件轉換爲字符串文件,或者讀取mp3文件並寫入文本文件,反之亦然?將音頻,mp3文件轉換爲字符串,反之亦然

如果可能的話怎麼樣?此外,文本文件的大小是否會大於或等於原始的mp3或音頻文件?

+0

你是指代表什麼的字符串?文件的二進制內容還是對音頻的一些理解?還有別的嗎? –

+0

字節或任何東西代表,但輸出與輸入相同,而字符串創建音頻或MP3文件創建時間 – Pratik

+0

你試圖問的東西有點不清楚。你想知道你是否可以將音頻文件轉換爲字符串或類似的東西? – deztructicus

回答

2

一個mp3文件就像所有文件都是簡單的二進制編碼數據,根據您用來查看數據的不同可以對其進行不同的解釋。

如果您問是否可以將音頻文件轉換爲字符串,那麼這是一個不正確的問題,因爲二進制數據只有在給定字符編碼時纔會被視爲字符串(您可以在記事本中打開mp3文件, '如果你想要'讀'')。

但是,如果你問如何從一個mp3文件讀取並寫入另一個文件,那麼這是它的代碼。

public String readFile(String filename) { 

    // variable representing a line of data in the mp3 file 
    String line = ""; 

    try { 
     br = new BufferedReader(new FileReader(new File(filename))); 

     while (br.readLine() != null) { 
      line+=br.readLine 

     try { 
      if (br == null) { 

       // close reader when all data is read 
       br.close(); 
      } 

     } catch (FileNotFoundException e) { 
      e.getMessage(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (FileNotFoundException e) { 
     e.getMessage(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

然後通過調用第二個文件中的文件讀取器方法寫入另一個文件。

public void outputData(String outputFile) { 

      try { 

     // Create file 
     FileWriter fileStream = new FileWriter(outputFile); 
     BufferedWriter writer = new BufferedWriter(fileStream); 

     writer.write(readFile(THE MP3 FILE DIRECTORY)); 

     // Close writer 
     writer.close(); 

     // Handle exceptions 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
2

文件是字節,當然可以解釋爲給定編碼中的字符。

我建議在這裏使用Base64編碼。在這種情況下,輸出文件將增大33%。

相關問題