
Ayuda! Como busco un dato dentro de un archivo
Publicado por Shannon (1 intervención) el 17/11/2017 03:29:28
Estoy realizando un programa donde tenga un menu donde pueda leer el archivo de texto pero quiero buscar un dato dentro del archivo de texto y me lo muestre, realice un metodo que se llama buscar, pero siempre me lo identifica con que no existe... ayuda
Clase 1:
Clase 2
Clase 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package menutexto;
import java.io.FileNotFoundException;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.File;
import java.util.*;
public class MetodosTexto
{
Scanner lee = new Scanner(System.in);
BufferedReader reg=null;
FileReader Mexico=null;
String registro="";
File archivo=null;
int c=0;
public int lee(String FileRead) //cuenta el numero de registros
{
try
{
archivo = new File(FileRead);
Mexico= new FileReader(archivo);
reg=new BufferedReader(Mexico);
while((registro=reg.readLine())!=null)
{
c=c+1;
reg.readLine();
}
}
catch (Exception e)
{
System.out.println("No leyo, quizas no encontro el archivo");
}
finally
{
try
{
if(null!=Mexico)
{
Mexico.close();
}
}
catch(Exception e)
{
System.out.println("Error en total de registros");
}
}
return c;
}
public void asigna(String datos[][], String FileRead)
{
int nr=0;
Campos invo = new Campos();
try
{
archivo = new File(FileRead);
Mexico= new FileReader(archivo);
reg=new BufferedReader(Mexico);
while((registro=reg.readLine())!=null)
{
invo.descompone(registro,datos,nr);
nr++;
reg.readLine();
}
}
catch (Exception e)
{
System.out.println("Error al descomponer");
}
finally
{
try
{
if(null!=Mexico)
{
Mexico.close();
}
}
catch(IOException ex)
{
System.out.println("Error en total de registros");
}
}
}
public void buscar(String CP, String datos[][], String FileRead)
{
int nr=0, r=0;
boolean bandera=false;
String cods[]= new String[datos.length];
String com="";;
for(int x=0; x<datos.length; x++)
{
if(datos[x][0]==CP)
{
System.out.println(x + "" + datos[x][1]);
cods[x]=datos[x][1];
String resto=datos[x][2]+"|"+datos[x][3]+"|"+datos[x][4];
bandera=true;
}
}
if(bandera==false)
{
System.out.println("No se encontro el domicilio");
}
}
}
Clase 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package menutexto;
public class Campos
{
public void descompone(String regis, String datos[][], int nreg)//numero de registros=nreg
{
String[] signifi={"C.P","Colonia","Municipio","Ciudad","Estado"};
String separador = "|";
String a="";
int pos=0, i=0;
while(regis.indexOf(separador)>-1)
{
a=regis.substring(0,regis.indexOf(separador)); //El registro que estoy obteniendo
datos[nreg][i]=a;
pos=regis.indexOf(separador)+separador.length();
regis=regis.substring(pos,regis.length());
i++;
}
datos[nreg][i]=regis;
}
}
Valora esta pregunta


0