【问题标题】:Sound distortion when extracting from Jar从 Jar 中提取时声音失真
【发布时间】:2022-01-24 17:34:24
【问题描述】:

我的 jar 文件目录中有声音。 我需要使用这些声音,我正在尝试使用这种方法提取它们:

String charset = "ISO-8859-1";
public void extractSounds(String pathIn, String pathOut) throws IOException {
    BufferedReader r = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(pathIn), charset));
    String line = r.readLine();
    String result = null;
    FileOutputStream fos = new FileOutputStream(pathOut);
    while(line != null) {
        if(result != null) {
            result += "\r" + line;
            line = r.readLine();
        } else {
            result = line;
            line = r.readLine();
        }
    }
    fos.write(result.getBytes(charset));
}}

但是当我提取声音时,它们会失真,我不知道问题出在哪里,因为它基本上只是复制文件。 听起来: Original, Extracted

如果您能帮我找到解决方案或提出另一种提取声音文件的方法,我将不胜感激。

【问题讨论】:

  • 您将二进制数据视为文本!您假设它被换行符分解,这仅对文本有意义。尝试以缓冲区的形式读取和写入数据。

标签: java file


【解决方案1】:

不要假设您正在阅读文本。您不应该尝试改变数据。分块复制即可。

试试类似的东西

InputStream in = ...;
ByteArrayOutputStream out = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
    out.write(buffer, 0, bytesRead);
}

【讨论】:

  • 我试过你的方法,但是没用。它给出了与我的代码相同的结果。也许我应该使用另一种写入方法而不是 FileInputStream?
  • 谢谢,我应该使用 DataOutputStream。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 2021-01-25
  • 2015-09-14
  • 2012-11-11
相关资源
最近更新 更多