0

それで、配列に何行あるか数えようとしているのですが、やり方がわかりません。このコードを書きました。ファイルを読み取り、コンテンツを配列に入れます。配列内の行をカウントするにはどうすればよいですか?

public static void main(String[] args) throws IOException {

    File file = new File("array_list.csv");
    FileInputStream fin = null;

    try {

        fin = new FileInputStream(file);
        byte fileContent[] = new byte[(int)file.length()];
        fin.read(fileContent);
        String fileToArray = new String(fileContent);


        //print file
        System.out.println(fileToArray);
    } 

    finally {
        if (null != fin) {
            fin.close();
        }
    }
}

ファイルarray_list.csvの内容は次のとおりです

569, 985, 97, 13,-94,256, 31, 74, 569,
-45, 601, 29, 19,-1952, 88,256,-69, 601,
1952, 1952,-15, 5,-54, 60, 89, 91, 39,
-601,-97, -6,-26, 89,-66,-601, 26, 28,
-51,-91, 39, 88, 99, 985, -7, 39,-29,
92, 1, 92, 92, 27,-11,-601, 22,-58,
13,-92, 28,-40, 27,-73, 0, 22, 33

fileToArray.length ビット explipse で試してみましたが、例外が返されます

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
length cannot be resolved or is not a field
4

4 に答える 4

0

fileToArray.length にする必要があります。

于 2013-10-18T12:18:10.713 に答える
0

この文字列パラメーターを以下のコードに渡すと、機能するはずです。

int count = new StringTokenizer(fileToArray, "\r\n").countTokens();

System.out.println(count);
于 2013-10-18T12:27:00.867 に答える
0

fileToArray 文字列をカンマで分割して長さを数えてみてはいかがでしょうか?

int rowCount = fileToArray.split(",").length
于 2013-10-18T12:27:21.427 に答える
0

実際にコンテンツを文字列に変換しました:

String fileToArray = new String(fileContent)

したがって、実際には文字列内の行 ( ) をカウントする必要が'\n'あります。あなたはそれを行うことができます

System.out.println(d.replaceAll("\n","").length() - d.replaceAll("\n","").length());
于 2013-10-18T12:29:54.100 に答える