如何使用此代码在 RandomAccessFile 中搜索字符串?

发布于 2024-12-20 05:58:27 字数 1939 浏览 4 评论 0原文

我正在尝试制作一个小搜索方法,到目前为止我有一个仅搜索 int 类型的方法,我一直想知道是否可以以某种方式重新实现此方法来搜索字符串?

//Searches only ints
public void buscarCliente_Codigo(int cod) throws IOException{ /*cod is for the input
for eg. a textfield input which im using*/

try{
RandomAccessFile f = new RandomAccessFile("Clientes.txt","rw"); //.txt File name
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
nombre = leerNombre(f);    //Reads String
numTelefono = leerNumTelefono(f);
direccion = leerDireccion(f);
seguro = leerSeguro(f);
nacionalidad = leerNacionalidad(f);
cedula = leerCedula(f);
if(cod==codigo){
iCodigoBusqueda = codigo;
encontrado=true;
registroExistente = true;
break;
}else{
iCodigoBusqueda = 0;  //Type int
registroExistente = false;
}
bytes +=1;//Changes
f.seek(bytes);
}
while(bytes<f.length());
f.close();
}  catch (Exception e){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Data not found");

}
}

完成此操作后,我只编写了几个 .getText 代码,并且有了一个仅适用于数字的漂亮搜索机制,但我真的想要一个搜索字符串的方法,所以我花了几个星期:

**enter code here**
public void buscarCliente_Nombre(String nom) throws IOException{ //change to String

try{
RandomAccessFile f = new RandomAccessFile("Clientes.txt","rw");
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
nombre = leerNombre(f);
numTelefono = leerNumTelefono(f);
direccion = leerDireccion(f);
seguro = leerSeguro(f);
nacionalidad = leerNacionalidad(f);
cedula = leerCedula(f);
if(nom.equals(nombre)){
iNombreString = nombre;
encontrado=true;
registroExistente = true;
break;
}else{
iNombreString = ""; //String type
registroExistente = false;
}
bytes +=1;//cambios
f.seek(bytes);
}
while(bytes<f.length());
f.close();
}  catch (Exception e){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Data not found");

}

}

它不起作用,它给了我例外。

那么这段代码是否适用于 Strings_?

谢谢

I am trying to make a little search method, and up to now I have one that only searches int types, I've been wondering if its somehow possible to re implement this method to search Strings?

//Searches only ints
public void buscarCliente_Codigo(int cod) throws IOException{ /*cod is for the input
for eg. a textfield input which im using*/

try{
RandomAccessFile f = new RandomAccessFile("Clientes.txt","rw"); //.txt File name
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
nombre = leerNombre(f);    //Reads String
numTelefono = leerNumTelefono(f);
direccion = leerDireccion(f);
seguro = leerSeguro(f);
nacionalidad = leerNacionalidad(f);
cedula = leerCedula(f);
if(cod==codigo){
iCodigoBusqueda = codigo;
encontrado=true;
registroExistente = true;
break;
}else{
iCodigoBusqueda = 0;  //Type int
registroExistente = false;
}
bytes +=1;//Changes
f.seek(bytes);
}
while(bytes<f.length());
f.close();
}  catch (Exception e){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Data not found");

}
}

After doing that I just code a couple of .getText and I have a beautiful search mechanism that only works with number, but I really want a method that searches for Strings, So I made a couple of tweeks:

**enter code here**
public void buscarCliente_Nombre(String nom) throws IOException{ //change to String

try{
RandomAccessFile f = new RandomAccessFile("Clientes.txt","rw");
boolean encontrado=false;
registroExistente = false ;
long bytes = 0;
do{
codigo = f.readInt();
nombre = leerNombre(f);
numTelefono = leerNumTelefono(f);
direccion = leerDireccion(f);
seguro = leerSeguro(f);
nacionalidad = leerNacionalidad(f);
cedula = leerCedula(f);
if(nom.equals(nombre)){
iNombreString = nombre;
encontrado=true;
registroExistente = true;
break;
}else{
iNombreString = ""; //String type
registroExistente = false;
}
bytes +=1;//cambios
f.seek(bytes);
}
while(bytes<f.length());
f.close();
}  catch (Exception e){
registroExistente = false;
JOptionPane.showMessageDialog(null,"Data not found");

}

}

It doesn't work, it gives me the Exception.

So is there anyway this code would work for Strings_?.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚我心虐我身 2024-12-27 05:58:27

你能在字符串中找到子字符串吗?是的。

老实说,我不知道你想在那里做什么,或者我知道“RandomAccessFile”是什么,但如果它只是一个常规文件,为什么不像读取它一样。

//Read the file... I don't bother to show how...
string = //the data of the file...
if(string.contains("Substring")) {
System.out.println("Substring could be found");
}
else {
System.out.println("Substring could not be found");
}

如果你想读取像 xml 这样的东西,我建议

if(string.contains("Substring=\"")) {
int pos = string.indexOf("Substring=\"")+"Substring=\"".length();
return string.substring(pos,string.indexOf("\"",pos));
}

You can find substrings in a string? yes.

Honestly i have no idea what your trying to do there, and either I know what a "RandomAccessFile" is but if it's just a regular file, why not just read it like one.

//Read the file... I don't bother to show how...
string = //the data of the file...
if(string.contains("Substring")) {
System.out.println("Substring could be found");
}
else {
System.out.println("Substring could not be found");
}

If you're trying to read something like xml, I'd recommend

if(string.contains("Substring=\"")) {
int pos = string.indexOf("Substring=\"")+"Substring=\"".length();
return string.substring(pos,string.indexOf("\"",pos));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文