第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在java中讀取文本文件中的列數(shù)

如何在java中讀取文本文件中的列數(shù)

30秒到達戰(zhàn)場 2022-07-20 21:02:34
如何在java中讀取文本文件中的列數(shù)。示例文本文件如下。逗號分隔。在此我需要將總列作為計數(shù) 4ABC,BBC,12-10-2018,1234ABC,BBC,12-10-2018,1234ABC,BBC,12-10-2018,1234ABC,BBC,12-10-2018,1234
查看完整描述

1 回答

?
動漫人物

TA貢獻1815條經(jīng)驗 獲得超10個贊

最簡單的方法是使用掃描儀并讀取第一行。

通過使用split()with,作為分隔符,您可以獲得一個數(shù)組,它的長度就是您想要的。


public static int getFileColumnsNumber(String filename) {

    File file = new File(filename);

    Scanner scanner;

    try {

        scanner = new Scanner(file);

    } catch (FileNotFoundException e) {

        e.printStackTrace();

        return -1;

    }


    int number = 0;

    if (scanner.hasNextLine()) {

        number = scanner.nextLine().split(",").length;

    }

    scanner.close();

    return number;

}


public static void main(String[] args) {

    String filename = "test.txt";

    System.out.println(getFileColumnsNumber(filename));

}


查看完整回答
反對 回復(fù) 2022-07-20
  • 1 回答
  • 0 關(guān)注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號