modificar un archivo txt que ya tiene datos
Publicado por Dannier marin (1 intervención) el 07/04/2021 02:47:49
Necesito modificar un archivo de txt, que ya tiene datos(estos datos son preguntas) ocupo agregar mas preguntas desacuerdo a la categoría (mate,ingles,etc) dentro de esta dividia en (primer examen,segundo examen)
Tengo ese codigo, pero no me busca la parte exacte que necesito
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
String rp;
int cant;
boolean encontrado = false;
ArrayList<String> al = new ArrayList<String>();
File f=new File("D:\\Datos.txt");
FileWriter fichero = null;
PrintWriter pw = null;
String inf;
int cont=1;
if(f.exists()){
try{
System.out.println("Ingrese la materia");
String buscar = obj.next();
System.out.println("Ingrese el grado");
String buscar2 = obj.next();
FileReader fr=new FileReader(f);
BufferedReader b = new BufferedReader(fr);
while((inf=b.readLine())!=null){
String[] n = inf.split("#");
if ((!n[0].equalsIgnoreCase(buscar))&& !n[0].equalsIgnoreCase(buscar2))
{
al.add(inf);
}
else
{
al.add(inf);
System.out.println("ingrese la pregunta ");
rp = obj.nextLine();
//System.out.println("Encontra el registro");
al.add(rp );
encontrado = true ;
}
}
if ( encontrado == false) {
System.out.println("Cita no encontrada");
}else{
System.out.println("Cita modificada con exito");
}
fr.close();
b.close();
File newfile = new File("D:\\Datos.txt");
newfile.delete();
fichero = new FileWriter("D:\\Datos.txt");
pw = new PrintWriter(fichero);
for (int i = 0; i < al.size(); i++)
pw.println(al.get(i).toString());
pw.close();
fichero.close();
}catch(IOException ex){ex.printStackTrace();}
}
else
{ System.out.println("Archivo no exite");}
}
Tengo ese codigo, pero no me busca la parte exacte que necesito
Valora esta pregunta


0