Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Beginner Web en 9 Septiembre 2019, 00:02 am



Título: Como guardar un JTextArea en un archivo txt usando itemMenu y actionListener
Publicado por: Beginner Web en 9 Septiembre 2019, 00:02 am
Código
  1. public void actionPerformed(ActionEvent e) {
  2.        if (e.getSource() == guardar) {
  3.            archivo = seleccion.getSelectedFile();
  4.            if (archivo.getName().endsWith("txt")) {
  5.                String documento = textarea.getText();
  6.                String mensaje = guardarArchivo(archivo, documento);
  7.                if (mensaje != null) {
  8.                    JOptionPane.showMessageDialog(null, mensaje);
  9.                } else {
  10.                    JOptionPane.showMessageDialog(null, "Archivo no compatible");
  11.                }
  12.            } else {
  13.                JOptionPane.showMessageDialog(null, "Guardar documento de texto");
  14.            }
  15.        }
  16.    }
  17.  
  18.    public String abrirArchivo(File archivo) {
  19.        String documento = "";
  20.        try {
  21.            entrada = new FileInputStream(archivo);
  22.            int ascii;
  23.            while ((ascii = entrada.read()) != -1) {
  24.                char caracter = (char) ascii;
  25.                documento += caracter;
  26.            }
  27.        } catch (Exception e) {
  28.        }
  29.        return documento;
  30.    }
  31.  
  32.    public String guardarArchivo(File archivo, String documento) {
  33.        String mensaje = null;
  34.        try {
  35.            salida = new FileOutputStream(archivo);
  36.            byte[] bytxt = documento.getBytes();
  37.            salida.write(bytxt);
  38.            mensaje = "Archivo guardado";
  39.        } catch (Exception e) {
  40.  
  41.        }
  42.        return mensaje;
  43.    }
  44. }

Solo tengo eso, y si se pyede elegir la ubicacion donde guardar el archivo


Título: Re: Como guardar un JTextArea en un archivo txt usando itemMenu y actionListener
Publicado por: rub'n en 13 Septiembre 2019, 14:58 pm
Código
  1. public void actionPerformed(ActionEvent e) {
  2.        if (e.getSource() == guardar) {
  3.            archivo = seleccion.getSelectedFile();
  4.            if (archivo.getName().endsWith("txt")) {
  5.                String documento = textarea.getText();
  6.                String mensaje = guardarArchivo(archivo, documento);
  7.                if (mensaje != null) {
  8.                    JOptionPane.showMessageDialog(null, mensaje);
  9.                } else {
  10.                    JOptionPane.showMessageDialog(null, "Archivo no compatible");
  11.                }
  12.            } else {
  13.                JOptionPane.showMessageDialog(null, "Guardar documento de texto");
  14.            }
  15.        }
  16.    }
  17.  
  18.    public String abrirArchivo(File archivo) {
  19.        String documento = "";
  20.        try {
  21.            entrada = new FileInputStream(archivo);
  22.            int ascii;
  23.            while ((ascii = entrada.read()) != -1) {
  24.                char caracter = (char) ascii;
  25.                documento += caracter;
  26.            }
  27.        } catch (Exception e) {
  28.        }
  29.        return documento;
  30.    }
  31.  
  32.    public String guardarArchivo(File archivo, String documento) {
  33.        String mensaje = null;
  34.        try {
  35.            salida = new FileOutputStream(archivo);
  36.            byte[] bytxt = documento.getBytes();
  37.            salida.write(bytxt);
  38.            mensaje = "Archivo guardado";
  39.        } catch (Exception e) {
  40.  
  41.        }
  42.        return mensaje;
  43.    }
  44. }

Solo tengo eso, y si se pyede elegir la ubicacion donde guardar el archivo

Con el listener del item menú, obtén el String y escríbelo.
Puedes usar JFileChooser para escojer la ubicación del archivo final

Tengo ejemplos con JFileChooser por aquí.

PD: cuando manejes excepciones, se más específica, en este caso en vez de usar la clase Exception usa, IOException que se refiere a una posible excepción de lectura/escritura de datos, tanto en tiempo de compilación o runtime.