也许是一个愚蠢的问题,但它让我发疯。
我有一个readAudioDataFromFile
从 main 调用时不接收参数的函数 ()。它不会抛出任何编译错误、异常等,它只是返回错误的值。
这里有一些简化的代码:
Main
班级
package audioCheck;
import java.io.FileNotFoundException;
public class AudioCheck
{
public static void main(String[] args) throws FileNotFoundException
{
String filePath1 = "C:/file1.wav";
String filePath2 = "C:/file2.wav";
new readAudioDataFromFile(filePath1, filePath2);
}
}
ReadAudioDataFromFile
班级
package audioCheck;
import java.io.*;
public class readAudioDataFromFile {
public readAudioDataFromFile(String filePath1, String filePath2) throws FileNotFoundException {
// This is the part I hardcode for getting the correct results, instead of passing them as args.
// String filePath2 = "C:/file2.wav";
// String filePath1 = "C:/file1.wav";
FileInputStream ins2 = null;
FileInputStream ins1 = null;
File file2 = new File(filePath2);
File file1 = new File(filePath2);
int size2 = (int) file2.length();
int size1 = (int) file1.length();
byte [] buffer2=new byte [size2];
byte [] buffer1=new byte [size1];
try {
ins2= new FileInputStream(filePath2);
ins1= new FileInputStream(filePath1);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
//contar e imprimir buffer 2
ins2.read(buffer2, 0,size2);
int count2 =0;
for(int i=0; i<buffer2.length; i++)
{
if(buffer2[i]!=0)
count2++;
}
String data2 = String.valueOf((count2));
System.out.println(data2);
//contar e imprimir buffer1
ins1.read(buffer1, 0,size1);
int count1 =0;
for(int i=0; i<buffer1.length; i++)
{
if(buffer1[i]!=0)
count1++;
}
String data1 = String.valueOf((count1));
System.out.println(data1);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
问题是,当我对文件路径进行硬编码时readAudioDataFromFile
,它可以完美运行。以下是每种情况的输出。
尝试从 args 传递main
:
348
388
硬编码readAudioDataFromFile
:
1205739
1346847
谢谢你的建议