Sie sind auf Seite 1von 9

EJEMPLO DE ARCHIVOS SECUENCIALES BASICO ESCRITURA:

import import import import import

java.io.DataInputStream; java.io.FileInputStream; java.io.FileNotFoundException; java.io.FileOutputStream; java.io.IOException;

public class Secuencial { static FileOutputStream fo; FileInputStream fi; //String nomArchivo; public static void main(String[] args) throws IOException { try { fo=new FileOutputStream("JTA.inf",true); // EL PARAMETRO INICIAL ES EL ARCHIVO, PUEDE IR LA // UNIDAD, ejemplo: d:/datos.dat byte s[]; String dato=leer(); s=dato.getBytes(); fo.write(s); fo.write((byte)'\n'); fo.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("Error en apertura"); } } static String leer() throws IOException { System.out.println("Ingrese nombre:"); DataInputStream medio=new DataInputStream(System.in); String texto=medio.readLine(); return texto; } }

EJEMPLO DE LECTRUA
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Lectura { static FileInputStream fi; public static void main(String[] args) { try { fi=new FileInputStream("e:/JTA.inf"); int c; try { while((c=fi.read())!=-1) { System.out.print((char)c); } fi.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("error en lectura"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("error en apertura"); } } }

PRACTICA DE PREPARACION PARA EL EXAMEN: En base al ejemplo anterior, realizar un men con las siguientes opciones: MENU 1. 2. 3. 4. 5. 6. Definir nombre de archivo Agregar un nuevo dato Listar el archivo Buscar un nombre Eliminar un nombre Salir

NOTA. En archivos secuenciales no se puede realizar un borrado lgico, lo recomendable es el borrado fsico, para esto siga la siguiente lgica: Copiar a otro archivo temporal todos los nombres menos el que se quiere eliminar, luego borrar el archivo original y finalmente renombrar el archivo temporal con el nombre del archivo original.

EJEMPLO DE UN MENU import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;

public class Sec {

static FileOutputStream fo; static FileInputStream fi; static String nomFile; public static void main(String[] args) throws IOException { int op=0; while(op!=5) { menu(); op=leerOp(); switch(op) { case 1: {

nomFile=leer(); break; }

case 2: { cargaDatos(); break;

} case 3: { listaDatos(); break; }

} }

static void menu() { System.out.println(" MENU"); System.out.println(" 1. DAR NOMBRE DE ARCHIVO"); System.out.println(" 2. aGREGAR DATOS ");

System.out.println(" 3. LISTAR DATOS"); System.out.println(" 5. SALIR");

static void nombraFile() throws IOException { String nombre=leer();

static String leer() throws IOException {

DataInputStream input=new DataInputStream(System.in); String texto; texto=input.readLine(); return texto; }

static int leerOp() throws IOException { int e; DataInputStream input=new DataInputStream(System.in); String texto;

texto=input.readLine(); e=Integer.valueOf(texto); return e; }

static void cargaDatos() throws IOException { try { fo=new FileOutputStream("e:/"+nomFile,true); byte s[]; String dato=leer(); s=dato.getBytes(); fo.write(s); fo.write((byte)'\n'); fo.close();

} catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }

static void listaDatos() { try {

fi=new FileInputStream("e:/"+nomFile); int c; try { while((c=fi.read())!=-1) { System.out.print((char)c);

} } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

} catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

Das könnte Ihnen auch gefallen