gpt4 book ai didi

java - 将音频、mp3 文件转换为字符串,反之亦然

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:24:55 26 4
gpt4 key购买 nike

是否可以将音频 mp3 文件转换为字符串文件,或者读取 mp3 文件并写入文本文件,反之亦然?

如果可能的话怎么办?另外,文本文件的大小是否会大于或等于原始 mp3 或音频文件?

最佳答案

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

关于java - 将音频、mp3 文件转换为字符串,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7282014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com