Java 从文件中读取字符串数据

Java 从文件中读取字符串数据,java,bufferedreader,Java,Bufferedreader,这是我制作的从2个diff文本文件中搜索用户给定字符串的程序。它的工作很好,但只搜索第一行。 如果一个文件 1000 1001 1002 它只搜索1000个。既然您已经使用了.equals()方法,我如何继续使用下一行 public class Main { public static void main(String[] args) { int ch = 0; do { Scanner in = new Scanner(System.in); String s; System.out.

这是我制作的从2个diff文本文件中搜索用户给定字符串的程序。它的工作很好,但只搜索第一行。 如果一个文件 1000 1001 1002 它只搜索1000个。既然您已经使用了
.equals()
方法,我如何继续使用下一行

public class Main {


public static void main(String[] args)
{
int ch = 0;
do
{

Scanner in = new Scanner(System.in);

String s;
System.out.println("Enter the part number");
s=in.nextLine();

try{

  BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Ankit\\Documents\\NetBeansProjects\\tcs_1\\number.txt"));
  BufferedReader Br = new BufferedReader(new FileReader("C:\\Users\\Ankit\\Documents\\NetBeansProjects\\tcs_1\\number1.txt"));

  String strLine;
  int flag=0;
  while ((strLine = br.readLine()) != null)
  {
        if(strLine.equals(s))
        {
            flag=1;
            System.out.println ("Part Number exists in 1");
            break;
        }
        else
        {
            flag=0;
            System.out.println ("Part Number doesnot exist in 1");
            break;
        }


    }

    if(flag==0)
    {

        while ((strLine = Br.readLine()) != null)
        {
            if(strLine.equals(s))
            {
                System.out.println ("Part Number exists in 2");
                break;
            }
     else
            {
                System.out.println("File does not exist in 2");
                break;
     }

        }
    }
        System.out.println ("Do you want to continue-Press1 for yes and 2 for no");

        ch= in.nextInt();
        br.close();
        Br.close();

}
catch (Exception e)
{
    System.err.println("Error: " + e.getMessage());
  }
}
while(ch==1);
  }
}
在while循环中,它将在检查第一行后退出循环。试着消除断裂;如果您想阅读所有行。

因为您使用了

public class Main {


public static void main(String[] args)
{
int ch = 0;
do
{

Scanner in = new Scanner(System.in);

String s;
System.out.println("Enter the part number");
s=in.nextLine();

try{

  BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Ankit\\Documents\\NetBeansProjects\\tcs_1\\number.txt"));
  BufferedReader Br = new BufferedReader(new FileReader("C:\\Users\\Ankit\\Documents\\NetBeansProjects\\tcs_1\\number1.txt"));

  String strLine;
  int flag=0;
  while ((strLine = br.readLine()) != null)
  {
        if(strLine.equals(s))
        {
            flag=1;
            System.out.println ("Part Number exists in 1");
            break;
        }
        else
        {
            flag=0;
            System.out.println ("Part Number doesnot exist in 1");
            break;
        }


    }

    if(flag==0)
    {

        while ((strLine = Br.readLine()) != null)
        {
            if(strLine.equals(s))
            {
                System.out.println ("Part Number exists in 2");
                break;
            }
     else
            {
                System.out.println("File does not exist in 2");
                break;
     }

        }
    }
        System.out.println ("Do you want to continue-Press1 for yes and 2 for no");

        ch= in.nextInt();
        br.close();
        Br.close();

}
catch (Exception e)
{
    System.err.println("Error: " + e.getMessage());
  }
}
while(ch==1);
  }
}

在while循环中,它将在检查第一行后退出循环。试着消除断裂;如果您想读取所有行。

您应该使用Scanner not BufferedReader,因为它是一个较新的类 我觉得这项工作做得更好。尤其是自从你 已经在代码的其他地方使用了Scanner,因此导入了它。 下面是一个扫描器,它可以在文件存在时读取文件中的所有行 这是下一本要读的书

 break;
示例文本文件data1.txt:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class JavaApplication32 
{
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        Scanner scanner1 = null;
        Scanner scanner2 = null;
        String partCheck;
        String repeatLoop;
        boolean isInOne;
        boolean isInTwo;

        File file1 = new File("data1.txt");
        File file2 = new File("data2.txt");

        try
        {
            scanner1 = new Scanner(file1);
            scanner2 = new Scanner(file2);
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
            do
            {
                isInOne = false;
                isInTwo = false;
                System.out.println("Enter the part number");
                partCheck = keyboard.nextLine();
                while (scanner1.hasNextLine() && !isInOne)
                {
                    String line = scanner1.nextLine();
                    if(line.equals(partCheck))
                    {
                        System.out.println("Part Number exists in 1");
                        isInOne = true;
                    }
                }
                if(!isInOne)
                {
                    System.out.println("Part Number does not exist in 1");
                }
                while(scanner2.hasNextLine() && !isInOne && !isInTwo)
                {
                    String line = scanner2.nextLine();
                    if(line.equals(partCheck))
                    {
                        System.out.println("Part Number exists in 2");
                        isInTwo = true;
                    }
                }
                if(!isInTwo)
                {
                    System.out.println("Part Number does not exist in 2");
                }
                System.out.println("Do you want to continue? (Y/N)");
                repeatLoop = keyboard.nextLine();
            } while(repeatLoop.equalsIgnoreCase("y"));
        scanner1.close();
        scanner2.close();
    }
}
示例测试文件data2.txt

Test1
Test2
Test3
Test4
使用以下数据文件运行代码时的标准输出示例:

Check1
Check2
Check3
Check4
你也不应该把所有的读入信息放在一个循环中。通过 把“做”放在首位,你就能有效地不断创造一套新的工作 BufferedReaders,并将它们命名为相同的东西,并告诉它们执行相同的操作 同样的事情,然后告诉他们在第一次击中后打破。如果你 如果你真的摆脱了休息时间,你会有更多的问题
所有其他东西都在循环中,不应该在循环中。

您应该使用Scanner而不是BufferedReader,因为它是一个较新的类 我觉得这项工作做得更好。尤其是自从你 已经在代码的其他地方使用了Scanner,因此导入了它。 下面是一个扫描器,它可以在文件存在时读取文件中的所有行 这是下一本要读的书

 break;
示例文本文件data1.txt:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class JavaApplication32 
{
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        Scanner scanner1 = null;
        Scanner scanner2 = null;
        String partCheck;
        String repeatLoop;
        boolean isInOne;
        boolean isInTwo;

        File file1 = new File("data1.txt");
        File file2 = new File("data2.txt");

        try
        {
            scanner1 = new Scanner(file1);
            scanner2 = new Scanner(file2);
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
            do
            {
                isInOne = false;
                isInTwo = false;
                System.out.println("Enter the part number");
                partCheck = keyboard.nextLine();
                while (scanner1.hasNextLine() && !isInOne)
                {
                    String line = scanner1.nextLine();
                    if(line.equals(partCheck))
                    {
                        System.out.println("Part Number exists in 1");
                        isInOne = true;
                    }
                }
                if(!isInOne)
                {
                    System.out.println("Part Number does not exist in 1");
                }
                while(scanner2.hasNextLine() && !isInOne && !isInTwo)
                {
                    String line = scanner2.nextLine();
                    if(line.equals(partCheck))
                    {
                        System.out.println("Part Number exists in 2");
                        isInTwo = true;
                    }
                }
                if(!isInTwo)
                {
                    System.out.println("Part Number does not exist in 2");
                }
                System.out.println("Do you want to continue? (Y/N)");
                repeatLoop = keyboard.nextLine();
            } while(repeatLoop.equalsIgnoreCase("y"));
        scanner1.close();
        scanner2.close();
    }
}
示例测试文件data2.txt

Test1
Test2
Test3
Test4
使用以下数据文件运行代码时的标准输出示例:

Check1
Check2
Check3
Check4
你也不应该把所有的读入信息放在一个循环中。通过 把“做”放在首位,你就能有效地不断创造一套新的工作 BufferedReaders,并将它们命名为相同的东西,并告诉它们执行相同的操作 同样的事情,然后告诉他们在第一次击中后打破。如果你 如果你真的摆脱了休息时间,你会有更多的问题

所有这些其他的东西都在循环中,不应该在循环中。

你到底为什么要创建两个变量,它们只是大小写不同?这是一场可读性噩梦。为什么要使用整数来表示实际上是布尔值的内容?嘿,从用户处读入的值是否始终是整数,nextLine()值是否也始终是整数?我想我想知道为什么不使用nextInt()来省去解析和/或在整数字符串上使用字符串相等的麻烦?@JonSkeet对变量名称感到抱歉…但是使用boolean或integer来检查条件有关系吗?是的,我的主要问题是do-while循环,它封装了所有内容,这不是个好主意。你不太可能得到想要的效果。@user1896796我删除了我的答案。我刚刚运行了你的程序,它运行得很好。你只需要调整你的
do while循环
你到底为什么要创建两个变量,它们只是大小写不同?这是一场可读性噩梦。为什么要使用整数来表示实际上是布尔值的内容?嘿,从用户处读入的值是否始终是整数,nextLine()值是否也始终是整数?我想我想知道为什么不使用nextInt()来省去解析和/或在整数字符串上使用字符串相等的麻烦?@JonSkeet对变量名称感到抱歉…但是使用boolean或integer来检查条件有关系吗?是的,我的主要问题是do-while循环,它封装了所有内容,这不是个好主意。你不太可能得到想要的效果。@user1896796我删除了我的答案。我刚刚运行了你的程序,它运行得很好。您只需调整
do while循环
@user1896796,这就是为什么您只得到一个值+1@sage88删除break后,也只读取第一行。我使用break是因为没有break,所有的print语句都会被打印。@user1896796只读取一行的原因是,您将所有内容都放在do-while循环中,该循环会在每个循环中创建所有读卡器的新实例,从而使它们从一行开始再次声明。@user1896796,这就是为什么您只得到一个值+1@sage88删除break后,也只读取第一行。我使用break是因为没有break,所有的print语句都会被打印。@user1896796只读取一行的原因是,您将所有内容都放在do-while循环中,该循环会在每个循环中创建所有读卡器的新实例,从而使它们从一行开始再说一遍。@user1896796好的,我按答案编辑,这应该完全符合您的要求。通过将do while循环放置在应该的位置,它避免了重新创建所有文件和扫描程序实例。它也不使用break语句。你真的应该避免使用中断循环,这是一个糟糕的做法。如果必须这样做,请谨慎地执行。现在,如果我必须在其中添加else语句,则每次换行时都找不到它。我也不能使用[break]。@user1896796为什么每次换行时都打印出来找不到?找不到的打印语句在while循环之外。它将首先在整个文件中循环搜索零件号。如果找到了,就打印找到的,就这样。如果找不到它,它将打印“未找到”,然后继续搜索另一个文件,该文件也会执行相同的操作。您首先不应该使用break。@user1896796您需要澄清您希望该文件做什么。您想让它检查file1吗?如果它找到值print,则只在1中找到,其他什么都没有?或者,您希望它检查file1是否找到在1中找到的值print,然后检查它是否