Advertisement
Guest User

Untitled

a guest
May 24th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. import java.io.EOFException;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.Scanner;
  6.  
  7. public abstract class Gestor {
  8.  
  9.     private Scanner sc = new Scanner(System.in);
  10.     private RandomAccessFile fichero;
  11.     private int LEN;
  12.     private String path;
  13.  
  14.     public abstract Ficha readFicha();
  15.     public abstract void writeFicha(Ficha ficha) throws IOException;
  16.  
  17.     //numero = sc.next(); //se lee el entero a añadir en el fichero
  18.  
  19.     public void darAlta(Ficha ficha) {
  20.         try {
  21.             //se abre el fichero para lectura y escritura
  22.             fichero = new RandomAccessFile(path, "rw");
  23.             listarFichero(); //muestra el contenido original del fichero
  24.             System.out.print("Introduce un número entero para añadir al final del fichero: ");
  25.             fichero.seek(fichero.length()); //nos situamos al final del fichero
  26.             writeFicha(ficha);       //se escribe la ficha
  27.             listarFichero();//muestra el contenido del fichero después de añadir el número
  28.  
  29.         } catch (FileNotFoundException ex) {
  30.             System.out.println(ex.getMessage());
  31.         } catch (IOException ex) {
  32.             System.out.println(ex.getMessage());
  33.         } finally {
  34.             try {
  35.                 if (fichero != null) {
  36.                     fichero.close();
  37.                 }
  38.             } catch (IOException e) {
  39.                 System.out.println(e.getMessage());
  40.             }
  41.         }
  42.     }
  43.  
  44.     public void listarFichero() {
  45.         Ficha ficha;
  46.         try {
  47.             fichero.seek(0); //nos situamos al principio
  48.             while (true) {
  49.                 ficha = readFicha();  //se lee  un entero del fichero
  50.                 System.out.println(ficha);  //se muestra en pantalla
  51.             }
  52.         } catch (EOFException e) {
  53.             System.out.println("Fin de fichero");
  54.         } catch (IOException ex) {
  55.             System.out.println(ex.getMessage());
  56.         }
  57.     }
  58.  
  59.     public void accederFicha(int i) throws IOException {
  60.         fichero.seek(i*LEN - 1);
  61.         Ficha ficha = readFicha();
  62.         System.out.println(ficha);
  63.     }
  64.  
  65.     public void darBaja(int i) throws IOException {
  66.         fichero.seek(i*LEN - 1);
  67.         Ficha ficha = readFicha();
  68.         ficha.setDadoAlta(false);
  69.         fichero.seek(i*LEN - 1);
  70.         writeFicha(ficha);
  71.     }
  72.  
  73.     public void darBaja(Ficha ficha) throws IOException {
  74.         ficha.setDadoAlta(false);
  75.         fichero.seek(ficha.getIdeNumber()*LEN - 1);
  76.         writeFicha(ficha);
  77.     }
  78.  
  79.  
  80.     public void setFichero(RandomAccessFile fichero) {
  81.         this.fichero = fichero;
  82.     }
  83.  
  84.     public void setLEN(int LEN) {
  85.         this.LEN = LEN;
  86.     }
  87.  
  88.     public void setPath(String path) {
  89.         this.path = path;
  90.     }
  91.  
  92.     public void setSc(Scanner sc) {
  93.         this.sc = sc;
  94.     }
  95.  
  96.     public Scanner getSc() {
  97.         return sc;
  98.     }
  99.  
  100.     public int getLEN() {
  101.         return LEN;
  102.     }
  103.  
  104.     public RandomAccessFile getFichero() {
  105.         return fichero;
  106.     }
  107.  
  108.     public String getPath() {
  109.         return path;
  110.     }
  111. }
  112.  
  113.  
  114. import java.io.*;
  115. import java.util.Scanner;
  116.  
  117.  
  118. public class GestorProfesor extends Gestor {
  119.  
  120.     public GestorProfesor() throws FileNotFoundException {
  121.         this.setPath("");
  122.         this.setSc(new Scanner(System.in));
  123.         this.setFichero(new RandomAccessFile(this.getPath(), "rw"));
  124.         this.setLEN(97);
  125.     }
  126.  
  127.     public void menu() throws IOException {
  128.         loop:
  129.         while (true) {
  130.             System.out.println("Pulse 1 si desea escribir en el archivo.");
  131.             System.out.println("Pulse 2 si desea listar el archivo.");
  132.             System.out.println("Pulse 3 si desea salir.");
  133.  
  134.             int i = Integer.parseInt(String.valueOf(System.in.read()));
  135.  
  136.             switch(i) {
  137.                 case 1:
  138.                     Profesor profesor = new Profesor();
  139.                     profesor.alta();
  140.                     this.writeFicha(profesor);
  141.                 case 2:
  142.                     this.listarFichero();
  143.                 case 3:
  144.                     break loop;
  145.             }
  146.         }
  147.     }
  148.  
  149.     @Override
  150.     public Ficha readFicha() {
  151.         Profesor profesor = new Profesor();
  152.  
  153.         Scanner sc = this.getSc();
  154.  
  155.         boolean dadoAlta = sc.nextBoolean();
  156.         long ideNumber = sc.nextLong(); //se lee el entero a añadir en el fichero
  157.         String ideString = sc.nextLine();
  158.         String nombre = sc.nextLine();
  159.         String nif = sc.nextLine();
  160.         String telefono = sc.nextLine();
  161.         String email = sc.nextLine();
  162.  
  163.         profesor.setDadoAlta(dadoAlta);
  164.         profesor.setIdeNumber(ideNumber);
  165.         profesor.setIdeString(ideString);
  166.         profesor.setNombre(nombre);
  167.         profesor.setNif(nif);
  168.         profesor.setTelefono(telefono);
  169.         profesor.setEmail(email);
  170.  
  171.         return profesor;
  172.     }
  173.  
  174.     @Override
  175.     public void writeFicha(Ficha ficha) throws IOException {
  176.         FileOutputStream fichero = new FileOutputStream(this.getPath());
  177.         ObjectOutputStream oos = new ObjectOutputStream(fichero);
  178.         oos.writeObject(ficha);
  179.  
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement