Java 具有数组列表的I/O

Java 具有数组列表的I/O,java,string,arraylist,Java,String,Arraylist,我只是想知道是否有一种方法可以在文本文件中循环,直到找到一个特定的字符串。 例如,假设您有一个包含以下内容的文本文件: 香蕉 苹果 葡萄 甜瓜 橙色的 樱桃 草莓巧克力香草 我基本上想编写一个程序,在输入文件中循环,直到它到达用户指定的特定字符串,然后将下一行存储在数组列表中。所以,基本上说,如果我输入樱桃,我希望它存储在一个数组列表中的草莓,巧克力,香草。但就我的一生而言,我不知道如何做到这一点,所以任何事情都将受到感激。到目前为止,我所了解的情况如下 public static void m

我只是想知道是否有一种方法可以在文本文件中循环,直到找到一个特定的字符串。 例如,假设您有一个包含以下内容的文本文件:

香蕉

苹果

葡萄

甜瓜

橙色的

樱桃

草莓巧克力香草

我基本上想编写一个程序,在输入文件中循环,直到它到达用户指定的特定字符串,然后将下一行存储在数组列表中。所以,基本上说,如果我输入樱桃,我希望它存储在一个数组列表中的草莓,巧克力,香草。但就我的一生而言,我不知道如何做到这一点,所以任何事情都将受到感激。到目前为止,我所了解的情况如下

public static void main(String[] args) throws IOException {
        String line;
        ArrayList<String> names = new ArrayList<>();
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the input file: ");
        String input = in.next();
        FileReader file = new FileReader(input);

        BufferedReader reader = new BufferedReader(file);

        System.out.print("What fruit do you want: ");
        String fruit = in.next();
        line = reader.readLine();


        while ((line != null)) {
            if(line.equals(fruit){
          }
            }
publicstaticvoidmain(字符串[]args)引发IOException{
弦线;
ArrayList name=新的ArrayList();
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入文件:”);
字符串输入=in.next();
FileReader文件=新的FileReader(输入);
BufferedReader reader=新的BufferedReader(文件);
System.out.print(“您想要什么水果:”);
字符串fruit=in.next();
line=reader.readLine();
while((行!=null)){
如果(直线)等于(水果){
}
}

只需循环输入,直到遇到搜索的行,然后将随后找到的每一行存储在列表中

    String line;

    while((line = reader.readLine()) != null)
        if(line.equals(fruit))
            break;

    ArrayList<String> lines = new ArrayList<>();
    while((line = reader.readLine()) != null)
        lines.add(line);
字符串行;
而((line=reader.readLine())!=null)
如果(直线等于(水果))
打破
ArrayList行=新的ArrayList();
而((line=reader.readLine())!=null)
行。添加(行);

只需循环输入,直到遇到搜索的行,然后将随后找到的每一行存储在列表中

    String line;

    while((line = reader.readLine()) != null)
        if(line.equals(fruit))
            break;

    ArrayList<String> lines = new ArrayList<>();
    while((line = reader.readLine()) != null)
        lines.add(line);
字符串行;
而((line=reader.readLine())!=null)
如果(直线等于(水果))
打破
ArrayList行=新的ArrayList();
而((line=reader.readLine())!=null)
行。添加(行);

您可以使用
布尔值
指示何时找到该值,何时为真,您可以在列表中添加该行:

 boolean isFound = false;
 String line;
 while ((line=reader.readLine())!= null) {
       if(!isFound && line.equals(fruit){
         isFound = true;
       }
       else if (isFound){
         names.add(line);
       }
 }

您可以使用
布尔值
指示何时找到该值,何时为真,您可以在列表中添加该行:

 boolean isFound = false;
 String line;
 while ((line=reader.readLine())!= null) {
       if(!isFound && line.equals(fruit){
         isFound = true;
       }
       else if (isFound){
         names.add(line);
       }
 }

创建标志isFound。找到字符串时将其设置为true。 然后,它将在下一个循环中处理,并像这样设置为false

bool isFound=false;       
while ((line != null)) {
          if(isFound==true){
              names.add(string)
              isFound=false;
          }
            if(line.equals(fruit){
              isFound=true;
          }
            }

创建标志isFound。找到字符串时将其设置为true。 然后,它将在下一个循环中处理,并像这样设置为false

bool isFound=false;       
while ((line != null)) {
          if(isFound==true){
              names.add(string)
              isFound=false;
          }
            if(line.equals(fruit){
              isFound=true;
          }
            }

如果我正确理解你的问题,我有一个想法,你可以从

Scanner scanner = new Scanner(file);
List<String> list = new ArrayList<String>();
while (scanner.hasNextLine()) { // read the entire file into list but it consumes time especially if the file is big (not perfect choice)
  list.add(scanner.nextLine());
}

如果我正确理解你的问题,我有一个想法,你可以从

Scanner scanner = new Scanner(file);
List<String> list = new ArrayList<String>();
while (scanner.hasNextLine()) { // read the entire file into list but it consumes time especially if the file is big (not perfect choice)
  list.add(scanner.nextLine());
}
请查看:

public static void main(String[] args) throws IOException {
    String line;
    List<String> names = new ArrayList<>();
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the input file: ");
    String input = in.next();
    FileReader file = new FileReader(input);

    BufferedReader reader = new BufferedReader(file);

    System.out.print("What fruit do you want: ");
    String fruit = in.next();

    boolean found=false;
    while ((line = reader.readLine()) != null){
        if (line.equals(fruit) || found) {
            names.add(line);
            found=true;
        }
    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
弦线;
列表名称=新的ArrayList();
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入文件:”);
字符串输入=in.next();
FileReader文件=新的FileReader(输入);
BufferedReader reader=新的BufferedReader(文件);
System.out.print(“您想要什么水果:”);
字符串fruit=in.next();
布尔值=false;
而((line=reader.readLine())!=null){
如果(直线等于(水果)| |找到){
名称。添加(行);
发现=真;
}
}
}
查看:

public static void main(String[] args) throws IOException {
    String line;
    List<String> names = new ArrayList<>();
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the input file: ");
    String input = in.next();
    FileReader file = new FileReader(input);

    BufferedReader reader = new BufferedReader(file);

    System.out.print("What fruit do you want: ");
    String fruit = in.next();

    boolean found=false;
    while ((line = reader.readLine()) != null){
        if (line.equals(fruit) || found) {
            names.add(line);
            found=true;
        }
    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
弦线;
列表名称=新的ArrayList();
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入文件:”);
字符串输入=in.next();
FileReader文件=新的FileReader(输入);
BufferedReader reader=新的BufferedReader(文件);
System.out.print(“您想要什么水果:”);
字符串fruit=in.next();
布尔值=false;
而((line=reader.readLine())!=null){
如果(直线等于(水果)| |找到){
名称。添加(行);
发现=真;
}
}
}

找到输入项的索引,向其中添加
1
以获得所需的索引。那么,您基本上是说将输入文件中的所有内容存储在一个数组中,然后找到列表中显示的输入项的索引吗?找到输入项的索引,向其中添加
1
以获得所需的索引。那么,您是基本的吗ally说将输入文件中的所有内容存储在一个数组中,然后找到列表中出现的插补项的索引?这正是我所需要的。我感谢您的帮助。@bacon9乐意帮助:)这正是我所需要的。我感谢您的帮助。@bacon9乐于帮助:)这只在文件中的单词是唯一的情况下才有效。如果搜索的单词被重复,您的代码将删除第二个字符串,该字符串将成为输出的一部分。这是有意的。我想这是意料之中的。我这方面的白痴猜测。我也不确定。我只是想指出。应该在问题中澄清。这只在文件中的单词是唯一的情况下才有效。如果搜索的单词被重复,您的代码将删除第二个字符串,该字符串应该是输出的一部分。这是有意的。我想这是意料之中的。我这方面的猜测很愚蠢。我也不确定。我我只是想指出,应该在问题中澄清。