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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何跳過(guò)文件的第一行?

如何跳過(guò)文件的第一行?

吃雞游戲 2023-07-28 16:43:16
我從文件中讀取并存儲(chǔ)在數(shù)組中,文件的第一行僅包含&ldquo;1&rdquo;,第二行包含從空格鍵分割的字典單詞。那么如何從第二行讀取文件呢?try? ? ? ? {? ? ? ? ?File text = new File ("dictionary.txt");? ? ? ? ?Scanner file = new Scanner(new File("dictionary.txt"));? ? ? ? ?while(file.hasNextLine())? ? ? ? ?{? ? ? ? ? System.out.println("Level 1");? ? ? ? ? ?int level1 = file.nextInt();? ? ? ? ? ?file.nextLine();? ? ? ? ? ?for(int i = 1; i < 7; i++)? ? ? ? ? ? {? ? ? ? ? ? ?String [] array = content.split(" ");? ? ? ? ? ? ?String A = array[0];? ? ? ? ? ? ?String B = array[1];? ? ? ? ? ? ?String C = array[2];? ? ? ? ? ? System.out.println(B);? ? ? ? ? ?}? ? ? ? ?}? ? ? ? ?file.close();? ? ? ? }文件的格式是1
查看完整描述

2 回答

?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

只需使用計(jì)數(shù)器


int count = 0;

while(file.hasNextLine())

{

    count++;

    if (count <= 1) {

      file.nextLine ();

      continue;

    }

    ....

}


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

我實(shí)際上會(huì)使用text而不是重新定義File來(lái)構(gòu)造Scanner. 更喜歡try-with-Resources顯式關(guān)閉Scanner. 實(shí)際上分配content,不要硬編碼數(shù)組迭代的“魔法值”。基本上,類似


File text = new File("dictionary.txt");

try (Scanner file = new Scanner(text)) {

    if (file.hasNextLine()) {

        file.nextLine(); // skip first line.

    }

    while (file.hasNextLine()) {

        String content = file.nextLine();

        if (content.isEmpty()) {

            continue; // skip empty lines

        }

        String[] array = content.split("\\s+");

        for (int i = 0; i < array.length; i++) {

            System.out.println(array[i]);

        }

    }

} catch (Exception e) {

    e.printStackTrace();

}

如果使用 Java 8+,另一種選擇是使用流式Files.lines(Path)傳輸所有行(和skip(1)),例如


File text = new File("dictionary.txt");

try {

    Files.lines(text.toPath()).skip(1).forEach(content -> {

        if (!content.isEmpty()) {

            System.out.println(Arrays.toString(content.split("\\s+")));

        }

    });

} catch (IOException e) {

    e.printStackTrace();

}


查看完整回答
反對(duì) 回復(fù) 2023-07-28
  • 2 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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