从音频文件中提取数据字节时,以下两种实现有什么区别?
该文件是一个.wav文件,我想只提取数据,没有标题或任何其他东西.
实施1:
public byte[] extractAudioFromFile(String filePath) {
try {
// Get an input stream on the byte array
// containing the data
File file = new File(filePath);
final AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(file);
byte[] buffer = new byte[4096];
int counter;
while ((counter = audioInputStream.read(buffer, 0, buffer.length)) != -1) {
if (counter > 0) {
byteOut.write(buffer, 0, counter);
}
}
audioInputStream.close();
byteOut.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}// end catch
return ((ByteArrayOutputStream) byteOut).toByteArray();
}
Run Code Online (Sandbox Code Playgroud)
实施2:
public …Run Code Online (Sandbox Code Playgroud) Java代码:
import java.util.regex.*;
class Test {
public static void main(String[] args) {
String r = "\\bdog\\b";
Pattern p = Pattern.compile(r);
String text = "abc dog def";
System.out.println(p.matcher(text).matches());
}
}
Run Code Online (Sandbox Code Playgroud)
等效Perl代码:
$x = "abc dog def";
if ($x =~ /\bdog\b/) {
print "matches";
}
Run Code Online (Sandbox Code Playgroud)
Perl代码按预期运行,并打印字符串"abc dog def"与给定的正则表达式匹配.另一方面,Java代码表示该字符串与正则表达式不匹配.我在犯一些愚蠢的错误吗?有趣的是,如果我输入cmd行上的正则表达式(不是字符串文字),如Oracle Docs所示,那么它可以正常工作.
我花了很多时间来学习使用迭代实现/可视化动态编程问题,但我发现它很难理解,我可以使用memoization的递归实现相同的但是与迭代相比它很慢.
有人可以通过硬问题的例子或使用一些基本概念来解释相同的问题.像矩阵链乘法,最长的回文子序列和其他.我可以理解递归过程,然后记住重叠的子问题以提高效率,但我无法理解如何使用迭代来做同样的事情.
谢谢!
这是我的代码:
pubic String toString()
{
String str;
str = "RentalDays:" + RentalDays +"\nRenalRate:" + RentalRate + "\nVehicleID " + VehicleID;
return str;
}
Run Code Online (Sandbox Code Playgroud)
RentalDays,RentalRate和VehicleID都是代码中的整数
该javac编译器会发出此错误:
Tractor.java:67: error: ';' expected
pubic String toString()
^
Tractor.java:67: error: invalid method declaration; return type required
pubic String toString()
^
2 errors
Run Code Online (Sandbox Code Playgroud)
我对下一步该做什么一无所知,我想我需要覆盖tostring方法来显示int但我不确定如何,任何提示?