Search a big file in java

0 Vani Manikkam · January 29, 2016
i am trying to search an id in this file and display their  data(id may repeat)
it is not working

import java.util.ArrayList;
import java.util.Scanner;

public class Search {

static String fileName = "C:\\Users\\vani\\Desktop\\Ass\\Search\\src\\Id.txt"; 
static File file = new File(fileName);

public static void main(String[] args)

Scanner input = new Scanner(;
ArrayList<ClassificationRecord> scienceRecords = new ArrayList(); 
BufferedReader in = getReader(fileName); 
String id;
System.out.println("Enter id: ");
id= input.nextLine();
ClassificationRecord record = readRecord(in); 
while(record != null){ 
if ((record.recID == id)){
System.out.println("Id: "+record.recID+" name: "+record.getfield1());

record = readRecord(in); 


private static BufferedReader getReader(String name){ 
BufferedReader in = null; 
try { 
File file = new File(name); 
in = new BufferedReader( 
new FileReader(file) ); 

catch (FileNotFoundException e){ 
System.out.println("The file doesn't exist."); 

catch (IOException e) { 
System.out.println("I/O Error"); 

return in; 

private static ClassificationRecord readRecord(BufferedReader in){ 
String line = "", field1;
String[] data; 
String recID; 
line = in.readLine(); 
}catch (IOException e){ 
System.out.println("I/O Error"); 

if (line == null) 
return null; 
data = line.split("\t");// need to use regexp 
recID = data[0].trim();// file contains spaces 
field1 = data[1].trim(); 

return new ClassificationRecord(recID, field1);

public static class ClassificationRecord{ 
public String recID; 
public String field1; 

public ClassificationRecord(String id, String f1){ 
this.recID = id; 
this.field1 = f1;
public String getfield1(){
return field1;
public String getrecID(){
return recID;

sample of file:

1 root
2 Bacteria
6 Azorhizobium
7 Azorhizobium caulinodans
9 Buchnera aphidicola
10 Cellvibrio
11 Cellulomonas gilvus
13 Dictyoglomus
14 Dictyoglomus thermophilum
16 Methylophilus
17 Methylophilus methylotrophus
18 Pelobacter
19 Pelobacter carbinolicus
20 Phenylobacterium
21 Phenylobacterium immobile
22 Shewanella
23 Shewanella colwelliana
24 Shewanella putrefaciens
25 Shewanella hanedai

Post a Reply


Oldest  Newest  Rating
0 Nikita Volobuev · January 29, 2016
Firstly, there is an ArrayIndexOutOfBoundsException when I try to run your code.
You have:

data = line.split("\t");// need to use regexp
recID = data[0].trim();// file contains spaces
field1 = data[1].trim();

But in your sample of the file data and ids are separated with space. So it would be better to change this code to:

recID = line.substring(0, line.indexOf(" "));
field1 = line.substring(line.indexOf(" ") + 1);

Secondly, you save your recID in String variable.
And you don`t compare it correctly with the value which you are looking for.

if ((record.recID == id)){
System.out.println("Id: "+record.recID+" name: "+record.getfield1());

This code compares strings for reference equality (whether they are the same object).
In order to compare strings values you need to use equals() method:

if ((record.recID.equals(id))){
System.out.println("Id: "+record.recID+" name: "+record.getfield1());

There is also an alternative solution: change recID to integer and compare integers using ==
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator