从文件中进行二进制搜索

时间:2014-10-23 16:47:39

标签: java binary-search

如何使用二分搜索搜索数据?当if语句开始时,程序进入IOException块。它似乎没有尝试读取文件。

public void busquedaDicotomica1(String clave)
{
    String lectura1=null, lectura2=null ;
    long posicion;
    long tamaño;
    long midValue;
    boolean valido = true;
    try
    {
        rafObj = new RandomAccessFile("indice1.ind","r");
        tamaño = rafObj.length();
        midValue = tamaño/2;
        rafObj.seek(midValue);
        if(clave.equalsIgnoreCase(rafObj.readUTF()))
        {

            System.out.println("ok");
        }
        else
        {
            System.out.println("no");
        }
     }
    catch(EOFException eoe)
    {}
    catch(FileNotFoundException nfe)
    {
        VentanaPrincipal.setErrorText(dialogo);
    }
    catch(IOException ioe)
    {
        VentanaPrincipal.setErrorText(ioe.getMessage());
    }
}
}

0 个答案:

没有答案