功能没有得到参数

时间:2013-09-25 18:43:58

标签: java file function arguments

也许是一个愚蠢的问题,但这让我很生气。

我有一个函数(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上对文件路径进行硬编码时,它完美无缺。以下是每种情况的输出结果。

尝试从main传递args:

348
388

硬编码readAudioDataFromFile

1205739
1346847

谢谢你的建议

0 个答案:

没有答案