Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.EOFException;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.Scanner;
- public abstract class Gestor {
- private Scanner sc = new Scanner(System.in);
- private RandomAccessFile fichero;
- private int LEN;
- private String path;
- public abstract Ficha readFicha();
- public abstract void writeFicha(Ficha ficha) throws IOException;
- //numero = sc.next(); //se lee el entero a añadir en el fichero
- public void darAlta(Ficha ficha) {
- try {
- //se abre el fichero para lectura y escritura
- fichero = new RandomAccessFile(path, "rw");
- listarFichero(); //muestra el contenido original del fichero
- System.out.print("Introduce un número entero para añadir al final del fichero: ");
- fichero.seek(fichero.length()); //nos situamos al final del fichero
- writeFicha(ficha); //se escribe la ficha
- listarFichero();//muestra el contenido del fichero después de añadir el número
- } catch (FileNotFoundException ex) {
- System.out.println(ex.getMessage());
- } catch (IOException ex) {
- System.out.println(ex.getMessage());
- } finally {
- try {
- if (fichero != null) {
- fichero.close();
- }
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- public void listarFichero() {
- Ficha ficha;
- try {
- fichero.seek(0); //nos situamos al principio
- while (true) {
- ficha = readFicha(); //se lee un entero del fichero
- System.out.println(ficha); //se muestra en pantalla
- }
- } catch (EOFException e) {
- System.out.println("Fin de fichero");
- } catch (IOException ex) {
- System.out.println(ex.getMessage());
- }
- }
- public void accederFicha(int i) throws IOException {
- fichero.seek(i*LEN - 1);
- Ficha ficha = readFicha();
- System.out.println(ficha);
- }
- public void darBaja(int i) throws IOException {
- fichero.seek(i*LEN - 1);
- Ficha ficha = readFicha();
- ficha.setDadoAlta(false);
- fichero.seek(i*LEN - 1);
- writeFicha(ficha);
- }
- public void darBaja(Ficha ficha) throws IOException {
- ficha.setDadoAlta(false);
- fichero.seek(ficha.getIdeNumber()*LEN - 1);
- writeFicha(ficha);
- }
- public void setFichero(RandomAccessFile fichero) {
- this.fichero = fichero;
- }
- public void setLEN(int LEN) {
- this.LEN = LEN;
- }
- public void setPath(String path) {
- this.path = path;
- }
- public void setSc(Scanner sc) {
- this.sc = sc;
- }
- public Scanner getSc() {
- return sc;
- }
- public int getLEN() {
- return LEN;
- }
- public RandomAccessFile getFichero() {
- return fichero;
- }
- public String getPath() {
- return path;
- }
- }
- import java.io.*;
- import java.util.Scanner;
- public class GestorProfesor extends Gestor {
- public GestorProfesor() throws FileNotFoundException {
- this.setPath("");
- this.setSc(new Scanner(System.in));
- this.setFichero(new RandomAccessFile(this.getPath(), "rw"));
- this.setLEN(97);
- }
- public void menu() throws IOException {
- loop:
- while (true) {
- System.out.println("Pulse 1 si desea escribir en el archivo.");
- System.out.println("Pulse 2 si desea listar el archivo.");
- System.out.println("Pulse 3 si desea salir.");
- int i = Integer.parseInt(String.valueOf(System.in.read()));
- switch(i) {
- case 1:
- Profesor profesor = new Profesor();
- profesor.alta();
- this.writeFicha(profesor);
- case 2:
- this.listarFichero();
- case 3:
- break loop;
- }
- }
- }
- @Override
- public Ficha readFicha() {
- Profesor profesor = new Profesor();
- Scanner sc = this.getSc();
- boolean dadoAlta = sc.nextBoolean();
- long ideNumber = sc.nextLong(); //se lee el entero a añadir en el fichero
- String ideString = sc.nextLine();
- String nombre = sc.nextLine();
- String nif = sc.nextLine();
- String telefono = sc.nextLine();
- String email = sc.nextLine();
- profesor.setDadoAlta(dadoAlta);
- profesor.setIdeNumber(ideNumber);
- profesor.setIdeString(ideString);
- profesor.setNombre(nombre);
- profesor.setNif(nif);
- profesor.setTelefono(telefono);
- profesor.setEmail(email);
- return profesor;
- }
- @Override
- public void writeFicha(Ficha ficha) throws IOException {
- FileOutputStream fichero = new FileOutputStream(this.getPath());
- ObjectOutputStream oos = new ObjectOutputStream(fichero);
- oos.writeObject(ficha);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement