Datos personales

domingo, 6 de octubre de 2013

Septimo tutorial, Lectura y Escritura de Archivos .txt


En esta ocasion les muestro este tutorial de como leer y escribir archivos de texto desde consola en java, utilizando el JFileChooser de la clase File y el BufferedReader de la Clase IO, asi como tambien la implementacion de la lectura de las opciones del menu con el Scanner de la clase Util.


import java.io.*;
import java.util.Scanner;
import javax.swing.JFileChooser;

/**
 *
 * @author Override MMX
 */
public class Lectura_Escritura_txt
{
    public static void main(String [] args) throws IOException
    {
        Lectura_Escritura_txt let=new Lectura_Escritura_txt();
        let.menu();      
    }
 
    void menu() throws IOException
    {
        Scanner teclado=new Scanner(System.in);
        System.out.print("\n\nMENU\n\n");
        System.out.print("1.-LEER\n");
        System.out.print("2.-ESCRIBIR\n");
        System.out.print("3.-SALIR\n");
        int opc=teclado.nextInt();
     
        switch(opc)
        {
            case 1:
                lectura();
                break;
            case 2:
                escritura();
                break;
            case 3:
                System.exit(0);
                break;
        }
    }
 
    void lectura()throws IOException
    {
        JFileChooser jfc=new JFileChooser();
        jfc.showOpenDialog(null);
        BufferedReader archivo=new BufferedReader(new FileReader(jfc.getSelectedFile().getPath()));
     
        while(archivo.ready())
        {
            System.out.print(archivo.readLine()+"\n");
        }
        menu();
    }
 
    void escritura() throws IOException
    {
        Scanner teclado=new Scanner(System.in);
        JFileChooser jfc=new JFileChooser();
        jfc.showSaveDialog(null);
        FileWriter fw=new FileWriter(new File(jfc.getSelectedFile().getPath()));
     
        System.out.print("INTRODUCE PALABRA A ESCRIBIR :");
        String pal=teclado.next();
        fw.write(pal);
        fw.close();
        menu();
    }
}


VÍDEO TUTORIAL



Link: Archivo .JAVA
Link: Ejecutable .exe

Facebook: www.facebook.com/locoslosdos
E-Mail: Moradomx@gmail.com

No hay comentarios:

Publicar un comentario