从文件中读取字符串数据

时间:2013-01-17 16:40:23

标签: java bufferedreader

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);
  }
}

这是我用2个差异文本文件搜索给定字符串的用户的程序。它的工作正常,但只搜索第一行。 例如:如果文件有     1000     1001     1002 它只会搜索1000.如何进入下一行并继续使用.equals()方法?

2 个答案:

答案 0 :(得分:3)

你应该使用Scanner not BufferedReader,因为它是一个更新的类 我觉得这项工作做得更好。特别是因为你有 已经在代码中的其他位置使用了Scanner,从而导入了它。 下面是一个扫描仪,它将读取文件中的所有行 是下一个要读的。

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();
    }
}

示例文本文件data1.txt:

Test1
Test2
Test3
Test4

示例测试文件data2.txt

Check1
Check2
Check3
Check4

使用这些数据文件运行代码时的stdout示例:

run:
Enter the part number
Test1
Part Number exists in 1
Part Number does not exist in 2
Do you want to continue? (Y/N)
y
Enter the part number
Check1
Part Number does not exist in 1
Part Number exists in 2
Do you want to continue? (Y/N)
n
BUILD SUCCESSFUL (total time: 19 seconds)

您也不应该将所有读入的信息都放在循环中。通过 把它放在最顶层你有效地继续创建一组新的 BufferedReaders并将它们命名为相同的东西并告诉它们 同样的事情,然后告诉他们在第一次打击后打破。如果你 确实摆脱了自那以后你会遇到更多问题的突破 所有这些其他的东西都在它不应该的循环中。

答案 1 :(得分:1)

因为你已经使用过

 break;

在while循环中,它将在检查第一行后退出循环。尝试删除休息;如果你想阅读所有的行。