我的 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));
}}
但是當我提取聲音時,它們會失真,我不知道問題出在哪里,因為它基本上只是復制檔案。聲音: 原始, 提取
如果您能幫我找到解決方案或建議另一種提取聲音檔案的方法,我將不勝感激。
uj5u.com熱心網友回復:
不要假設您正在閱讀文本。你不應該試圖改變資料。只需將其分塊復制即可。
嘗試類似
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/393803.html
