Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class ArchivoSecuencial {
- //Escribir
- public static void escribirArchivo(String primerNombre, String segundoNombre, String primerApellido,
- String segundoApellido, String cui) {
- try {
- Persona persona = new Persona(primerNombre, segundoNombre, primerApellido, segundoApellido, cui);
- File file = new File("datos.txt");
- if (file.exists() && file.length() != 0) {
- FileWriter fileWriter = new FileWriter(file, true);
- BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
- bufferedWriter.write(persona.getPrimerNombre());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getSegundoNombre());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getSegundoApellido());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getPrimerApellido());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getSegundoApellido());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getCui());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.close();
- } else {
- FileWriter fileWriter = new FileWriter(file, false);
- BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
- bufferedWriter.write(persona.getPrimerNombre());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getSegundoNombre());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getSegundoApellido());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getPrimerApellido());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getSegundoApellido());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.write(persona.getCui());
- bufferedWriter.newLine();
- bufferedWriter.write("%");
- bufferedWriter.close();
- }
- } catch (Exception error) {
- System.err.println(error);
- }
- }
- public static void mostrarArchivo() {
- File file = new File("datos.txt");
- if (file.exists()) {
- try {
- FileReader fileReader = new FileReader(file);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- String[] arreglo = line.split("%");
- Persona persona = new Persona(arreglo[0], arreglo[1], arreglo[2], arreglo[3], arreglo[4]);
- System.out.println(persona.toString());
- }
- bufferedReader.close();
- } catch (Exception error) {
- System.err.println(error);
- }
- }
- }
- public static void buscarDatosArchivo(String primerNombre) {
- File file = new File("datos.txt");
- if (file.exists()) {
- try {
- FileReader fileReader = new FileReader(file);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- String[] arreglo = line.split("%");
- Persona persona = new Persona(arreglo[0], arreglo[1], arreglo[2], arreglo[3], arreglo[4]);
- if (primerNombre.equalsIgnoreCase(primerNombre)) {
- System.out.println(persona.toString());
- bufferedReader.close();
- return;
- }
- }
- System.out.println("No se encontró la persona en el archivo.");
- bufferedReader.close();
- } catch (Exception error) {
- System.err.println(error);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement