fileToSTring继续返回“”

时间:2010-04-26 00:33:17

标签: java file-io

我设法让这个代码编译出错。但不知怎的,它没有返回我在file1.txt和file.txt中写的字符串,我通过str1和str2传递它的路径。我的目标是使用这个开源库来测量包含2个文本文件的字符串之间的相似性。

在其Javadoc内部,其状态为......

public static java.lang.StringBuffer fileToString(java.io.File f)

private call to load a file and return its content as a string.

Parameters:
    f - a file for which to load its content 
Returns:
    a string containing the files contents or "" if empty or not present

这是我修改过的代码试图使用FileLoader函数,但无法返回文件中的字符串。最终的结果一直让我回归“”。我不知道我的错在哪里:

   package uk.ac.shef.wit.simmetrics;
   import java.io.File;
   import uk.ac.shef.wit.simmetrics.similaritymetrics.*;
   import uk.ac.shef.wit.simmetrics.utils.*;


public class SimpleExample {
  public static void main(final String[] args) {
    if(args.length != 2) {

        usage();

    } else {

        String str1 = "arg[0]";
        String str2 = "arg[1]";

        File objFile1 = new File(str1);

        File objFile2 = new File(str2);

        FileLoader obj1 = new FileLoader();
        FileLoader obj2 = new FileLoader();

        str1 = obj1.fileToString(objFile1).toString();

        str2 = obj2.fileToString(objFile2).toString();


        System.out.println(str1);            
        System.out.println(str2);


        AbstractStringMetric metric = new MongeElkan();

        //this single line performs the similarity test

        float result = metric.getSimilarity(str1, str2);

        //outputs the results

        outputResult(result, metric, str1, str2);

    }

}


private static void outputResult(final float result, final AbstractStringMetric metric, final String str1, final String str2) {

    System.out.println("Using Metric " + metric.getShortDescriptionString() + " on strings \"" + str1 + "\" & \"" + str2 + "\" gives a similarity score of " + result);



}



private static void usage() {

    System.out.println("Performs a rudimentary string metric comparison from the arguments given.\n\tArgs:\n\t\t1) String1 to compare\n\t\t2)String2 to compare\n\n\tReturns:\n\t\tA standard output (command line of the similarity metric with the given test strings, for more details of this simple class please see the SimpleExample.java source file)");

}

}

更新:我修改了代码,但它给了我这个错误:

SimpleExample.java:79: cannot find symbol
symbol  : variable arg
location: class uk.ac.shef.wit.simmetrics.SimpleExample
        String str1 = arg[0];
                      ^
SimpleExample.java:80: cannot find symbol
symbol  : variable arg
location: class uk.ac.shef.wit.simmetrics.SimpleExample
        String str2 = arg[1];
                      ^

1 个答案:

答案 0 :(得分:2)

如果您通过命令行运行Java程序,似乎您没有正确检索参数,

String str1 = "arg[0]";
String str2 = "arg[1]";

应该是,

String str1 = args[0];
String str2 = args[1];