PROGRAMA PER A LLEGIR I ESCRIURE FITXERS EN JAVA2

 

 

 

 

 

 

Ficheros

 

El codi és el següent:

 

//package guardar_leer_fichero;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

 

 

public class guardar_leer_fichero extends Frame implements ActionListener

{

      TextArea ta;

      TextField tf;

      Button b1,b2;

      Label l1,l2;

      File fichero;

 

      public guardar_leer_fichero()

      {

 

      }

      public void iniciar()

      {

            this.setTitle("Leer y Guardar");

            this.setLayout(null);

            ta=new TextArea();

            ta.setBounds(10,40,230,120);

            this.add(ta);

            l1=new Label("Nombre del fichero");

            l1.setBounds(10,160,100,20);

            this.add(l1);

            tf=new TextField();

            tf.setBounds(10,180,200,20);

            this.add(tf);

            b1=new Button("Guardar");

            b1.setBounds(10,220,70,20);

            b1.addActionListener(this);

            this.add(b1);

            b2=new Button("Leer");

            b2.setBounds(120,220,70,20);

            b2.addActionListener(this);

            this.add(b2);

 

            l2=new Label("");

            l2.setBounds(120,220,100,20);

            this.add(l2);

            this.addWindowListener(new Cerrar());

           

 

      }

      public void actionPerformed(ActionEvent ae)

      {

            if(ae.getSource()==b1)

            {

                  if(tf.getText().equals(""))

                  {

                  }

                  else

                  {

                        fichero= new File(".",tf.getText());

                        escribir_leer el=new escribir_leer();

                        el.escribir(ta.getText(),fichero);

                        tf.setText("Fichero Guardado");

                  }

            }

            if(ae.getSource()==b2)

            {

                  if(tf.getText().equals(""))

                  {

                  }

                  else

                  {

                        ta.setText("");

                        fichero= new File(".",tf.getText());

                        escribir_leer el=new escribir_leer();

                        ta.setText(el.leer(fichero));

                        tf.setText("Fichero Leido");

                  }

            }

      }

 

      public static void main(String[] args)

      {

            guardar_leer_fichero g = new guardar_leer_fichero();

            g.setSize(250,250);    

            g.iniciar();

            g.setVisible(true);

 

      }

      class Cerrar extends WindowAdapter

      {

            public void windowClosing(WindowEvent e)

            {

                  e.getWindow().dispose();

                  System.exit(0);

            }

      }

 

      public class escribir_leer

      {

            public escribir_leer(){}

 

            public String leer(File file)

            {

                  StringBuffer buffer = new StringBuffer();

                  String linea;

                  FileReader flector;

                  BufferedReader blector;

 

                  try

                  {

                        flector = new FileReader(file);

                        blector = new BufferedReader(flector);

                        while ((linea = blector.readLine()) != null)

                        {

                             buffer.append(linea);

                        }

                        blector.close();

                        flector.close();

                  }

                  catch (FileNotFoundException e)

                  {

                        e.printStackTrace();

                  }

                  catch (IOException e)

                  {

                        e.printStackTrace();

                  }

 

                  return buffer.toString();

            }

 

            public void escribir(String string, File file)

            {

                  FileWriter fescritor;

                  BufferedWriter bescritor;

 

                  try

                  {

                        fescritor = new FileWriter(file);

                        bescritor = new BufferedWriter(fescritor);

                        bescritor.write(string);

                        bescritor.close();

                        fescritor.close();

                  }

                  catch (IOException e)

                  {

                        e.printStackTrace();

                  }

            }

      }

 

 

}