Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 23rd, 2006, 05:09 PM
Authorized User
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Binary Search on a text file


Can someone please help me. I have a really large text file that contains a list of hash values, it has about ten million entries. I wanted to do a binary search on this to check if a particular hash value is present in the file and to return true if it is and false if not. i have tried to write a binary search and have a small text file with nine entries. the problem i am having is that i don't know how to get it to jump to the middle of the file to begin the search from there without loading it into memory.

any help would be greatly appreciated,



Here's the code i tried:

import java.io.*; 
import java.util.*;
class TokenizerExample3 
   public static String a[];
   public static int i=0;

   public static int binarySearch(String[] a2, String searchItem) 
    int first=0;
    int last = array.length - 1;
    int middle;

    boolean found = false;

    //Loop until found or end of list.
    while(first <= last &&!found) 
      middle = (first + last) /2;
      if(array[middle]==(searchItem)) found = true;

         last = middle -1;
         else first = middle + 1;
     }// end while

        if(found) return middle;
        else return(-1);
    }//end binary search

     /* Main method */
    public static void main(String[] args) throws IOException 
    FileReader file = new FileReader("C:\\test.txt");
    BufferedReader fileInput = new BufferedReader(file);
    long numLines = 0;

    String line;
       line = fileInput.readLine();
       if (line != null)
        a[i] = line;
        while (line != null);
        String searchItem = "hello"; 
                  //hello is at the 4th entry in the file
        binarySearch(a, searchItem);
}//end main    
}//end class
Reply With Quote
  #2 (permalink)  
Old August 25th, 2006, 12:43 AM
Authorized User
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts


i got it to jump to the middle of the file. does anyone know how i would extract the string located at this point and compare it to my test string.



Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search a binary file of million records DineshGirij008 C# 18 July 17th, 2008 09:38 AM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Conversion of Binary file to Text file suneet.pant Pro VB.NET 2002/2003 1 April 20th, 2007 07:29 AM
How to convert Binary file to Text File pbassett Beginning VB 6 1 May 9th, 2006 03:51 AM
SQL 2000 database / text file / binary file pallone Javascript How-To 3 January 28th, 2005 01:26 PM

All times are GMT -4. The time now is 03:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.