繁体   English   中英

如何在Java中的txt文件中跳过行

[英]how to skip lines in txt file in java

在下面您可以看到我的代码。 它读取sort.txt文件,其中的单词首先由长度格式化,然后像这样:

  • 一种
  • b
  • AB
  • AC
  • ABC
  • ACD
  • AAAA

每行一个字。 在下面的程序中,它将在数组list1中添加单词lenght 1,在数组list2中添加单词lenght 2。 它工作正常,但是当它读取时说arraylist3时,它将读取整个sort.txt。 我如何才能使它跳过单词1和2,直接转到单词3,并将其添加到list3。 然后,当发现第一个单词长度为4时,它停止。 所以我不必一遍又一遍地读取整个文件吗?

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class swithc {

public static void main(String[] args) {
String izbira;
int dolzina=0;
Scanner in = new Scanner(System.in);
String vnos;
Scanner input = new Scanner(System.in);

ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
ArrayList list4 = new ArrayList();
ArrayList list5 = new ArrayList();
ArrayList list6 = new ArrayList();
ArrayList list7 = new ArrayList();
ArrayList list8 = new ArrayList();
ArrayList list9 = new ArrayList();
ArrayList list10plus = new ArrayList();

try {

    File file = new File("sort.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String vrstica;

    while ((vrstica = bufferedReader.readLine()) != null) {
        if (vrstica.length() == 1) {
            list1.add(vrstica);
        }
        if (vrstica.length() == 2) {
            list2.add(vrstica);
        }
        if (vrstica.length() == 3) {
            list3.add(vrstica);
        }
        if (vrstica.length() == 4) {
            list4.add(vrstica);
        }
        if (vrstica.length() == 5) {
            list5.add(vrstica);
        }
        if (vrstica.length() == 6) {
            list6.add(vrstica);
        }
        if (vrstica.length() == 7) {
            list7.add(vrstica);
        }
        if (vrstica.length() == 8) {
            list8.add(vrstica);
        }
        if (vrstica.length() == 9) {
            list9.add(vrstica);
        }
        if (vrstica.length() > 9) {
            list10plus.add(vrstica);
        }
    }
    do{
        do {
            System.out.println("Vnesi dožino besede, ki jo iščeš:");
            if (in.hasNextInt()) {
                dolzina = in.nextInt();
            } else if (in.hasNextLine()) {
                System.out.printf("Napačen vnos! Poskusi ponovno:%n ",
                        in.nextLine());
            } 
        } while (dolzina <= 0);



    System.out.println("Vnesi besedo za neznano črko vpiši * :");
    vnos = input.nextLine();
    vnos = vnos.replace("*", ".");

    if (dolzina == 1) {
        for (int i = 0; i < list1.size(); i++) {
            String s = (String) list1.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }

    }

    if (dolzina == 2) {
        for (int i = 0; i < list2.size(); i++) {
            String s = (String) list2.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }

    }
    if (dolzina == 3) {

        for (int i = 0; i < list3.size(); i++) {
            String s = (String) list3.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 4) {

        for (int i = 0; i < list4.size(); i++) {
            String s = (String) list4.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 5) {
        for (int i = 0; i < list5.size(); i++) {
            String s = (String) list5.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 6) {
        for (int i = 0; i < list6.size(); i++) {
            String s = (String) list6.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 7) {
        for (int i = 0; i < list7.size(); i++) {
            String s = (String) list7.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 8) {
        for (int i = 0; i < list8.size(); i++) {
            String s = (String) list8.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 9) {
        for (int i = 0; i < list9.size(); i++) {
            String s = (String) list9.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina > 9) {
        for (int i = 0; i < list10plus.size(); i++) {
            String s = (String) list10plus.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }

    }
    dolzina=-1;
    System.out.println("Ponovni vnos (da/ne):");
    Scanner inn= new Scanner (System.in);
    izbira = inn.next();

}while (izbira.equalsIgnoreCase("da"));
    bufferedReader.close();
} catch (IOException e) {
    e.printStackTrace();

}
}}

您必须按顺序进行,但是可以更快地进行:

int TARGET_LEN = 3;
...
while ((vrstica = bufferedReader.readLine()) != null) {
    if (vrstica.length() < TARGET_LEN) {
        continue;
    } else if (vrstica.length() > TARGET_LEN) {
        break;
    }
    // Here you know that your word is of length TARGET_LEN
    // Add it to your list:
    list.add(vrstica); // You do not need many lists now, only one.
    // Do whatever else you may need to do.
}

你的意思是这样的吗? 这将跳过长度1和2,从而增加长度3,并在找到长度4时停止。

int num = 3;
while ((vrstica = bufferedReader.readLine()) != null) {
    //Do nothing for length 1 and 2
    if (vrstica.length() == num) {
        list3.add(vrstica);
    } else if (vrstica.length() > num) {
        //stop reading the file
        break;
    }
}

您可以使用break关键字来停止while循环中的操作。 如果您只想保留长度为3的单词:

while ((vrstica = bufferedReader.readLine()) != null) {
    if (vrstica.length() == 3) {
        list3.add(vrstica);
    }  else if (vrstica.length() == 4) {
        break;
    }
}

如果条件中的任何内容都不适合该长度的行,则无需检查一两个即可,因为读取的行将被丢弃。 请注意,此解决方案依赖于文件的精确排序,以便所有长度为4的字符串出现在所有长度为3的字符串之后。

好的,根据您的评论,似乎您在说出自己想要的内容时有些困惑。 如果要在一个列表中放入长度为1的单词,在下一个列表中放入长度为2的单词,依此类推,这是一个解决方案:

// note that ArrayList needs a type it will contain
ArrayList<ArrayList<string>> wordArrayLists = new ArrayList<ArrayList<string>>();

// fill the top ArrayList with 10 ArrayList<string>s
for (int i = 0; i < 10; i++) {
    wordArrayLists.add(ArrayList<string>());
}

while ((vrstica = bufferedReader.readline()) != null) {
    int index = vrstica.length - 1;

    // skip if the word length is 0 or greater than 10
    if (index < 0) { 
        continue;
    }

    if (index >= 10) {
        wordArrayLists(9).add(vrstica);
    }
    else {
        wordArrayLists.get(index).add(vrstica);
    }
}

读取完文件后, wordArrayLists的第一个索引将是仅包含1个长度字的ArrayList ,第二个索引将仅包含2个长度字, wordArrayLists ,直到第10个索引,该索引将包含所有字长度为10或更大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM