3

Is it possible to convert an audio mp3 file to a string file, or read the mp3 file and write in a text file and vice versa?

If possible then how? Also, would the text file size be greater or equal to the original mp3 or audio file?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • You mean a String representing what?.. The binary content of the file or some understanding of the audio? Something else? – Jean Logeart Sep 02 '11 at 10:16
  • byte or anything represent but output as same as the input while string to create the audio or mp3 file creation time – Pratik Sep 02 '11 at 10:19
  • What you're trying to ask is slightly unclear. Would you like to know if you can convert an audio file to a string or something of the sort? – Zeeno Sep 02 '11 at 10:58

2 Answers2

3

An mp3 file like, all files is simply binary encoded data which can be interpreted differently depending on what you use to view that data.

If you're asking if you can convert an audio file to a string then that is an incorrect question as binary data is only viewed as a string when given a character-encoding (you could open your mp3 file in notepad and 'read' it if you wanted).

However, if you're asking how you can read from an mp3 file and write to another file then this is code for it.

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

Then to write to another file by calling the file reader method in the second file.

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();
    }
}
Zeeno
  • 2,671
  • 9
  • 37
  • 60
2

Files are bytes, which of course can be interpreted as characters in a given encoding.

I'd suggest to use Base64 encoding here. In this case the output file will be 33% bigger.

Pratik
  • 30,639
  • 18
  • 84
  • 159
Mister Smith
  • 27,417
  • 21
  • 110
  • 193