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 o开发者_开发问答r equal to the original mp3 or audio file?
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();
}
}
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.
精彩评论