Package data

Source Code of data.FileControllerD

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package data;

/**
*
* @author Oriol
*/
import domain.FileControllerDomain;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.io.*;

import presentation.FileControllerP;

public class FileControllerD extends JFrame {

     private JFileChooser _fc;

        public FileControllerD() {
            setLayout(null);

        }


    /**
    *
    *
    * Obre el fitxer especificat mitjançant un FileChooser i el posa al textArea passat com a parà metre
    *
    * @author oriol.cano
    * @param JTextArea ta: Area de text on volem que s'obri el ficher.
    * @return -
    * @throws -
    */
        public void openFile(JTextArea ta) {
      try {

                     BufferedReader input = getFileR();
                     StringBuilder buffer = new StringBuilder();
                     String t;
                     ta.setText("");
                     while((t = input.readLine()) != null) {
                         buffer.append(t).append("\n");
                     }
                     ta.append(buffer.toString());



           }catch(IOException ioException) {
                JOptionPane.showMessageDialog(this,"Error canal: "+ ioException);
           }
    }

    /**
    *
    *
    * Retorna el fitxer de text seleccionat mitjançant un JFileChooser
    *
    * @author oriol.cano
    * @param -
    * @return BufferedReader amb el contingut del fitxer de text.
    * @throws -
    */
    public BufferedReader getFileR() {
            FileControllerDomain fCD = new FileControllerDomain();
            _fc = fCD.openFile();
            /*
             * LA FUNCIO DE SOBR S'HAURIA D'INVOCAR AL CONTROLADOR DE DOMINI
                PROPI DE CADA SUBGRUP, HE POSAT EL DE PRESENTACIO PER COMODITAT
             */
            File fitxer = _fc.getSelectedFile();

             if((fitxer == null) || (fitxer.getName().equals(""))) {
                JOptionPane.showMessageDialog(this,"Error","Error",JOptionPane.ERROR_MESSAGE);
                return null;
             }
      if(fitxer.isFile() && fitxer.canRead()) {

        BufferedReader _buf = null;
        try {
          _buf = new BufferedReader(new FileReader(fitxer));
        } catch (FileNotFoundException ex) {
          //Logger.getLogger(FileControllerD.class.getName()).log(Level.SEVERE, null, ex);
        }
          return _buf;
        }
      return null;
    }

    /**
    *
    *
    * Retorna un buffer de lectura del fitxer de text indicat per una ruta.
    *
    * @author oriol.cano
    * @param String path: Indica la ruta del fitxer de text.
    * @return BufferedReader amb el contingut del fitxer de text.
    * @throws -
    */
    public BufferedReader getFileR(String path) {
      File _file = new File(path);
      BufferedReader _buf = null;
      try {
        _buf = new BufferedReader(new FileReader(_file));
      } catch (Exception ex) {
        Logger.getLogger(FileControllerD.class.getName()).log(Level.SEVERE, null, ex);
      }
      return _buf;
    }

    /**
    *
    *
    * Retorna un buffer d'escriptura del fitxer de text seleccionat mitjançant un JFileChooser.
    *
    * @author oriol.cano
    * @param -
    * @return BufferedWriter des d'on es pot afegir contingut al fitxer de text.
    * @throws -
    */
     public BufferedWriter getFileW() {
            FileControllerDomain fCD = new FileControllerDomain();
            _fc = fCD.openFile();
            /*
             * LA FUNCIO DE SOBRE S'HAURIA D'INVOCAR AL CONTROLADOR DE DOMINI
                PROPI DE CADA SUBGRUP, HE POSAT EL DE PRESENTACIO PER COMODITAT
             */
            File fitxer = _fc.getSelectedFile();

             if((fitxer == null) || (fitxer.getName().equals(""))) {
                JOptionPane.showMessageDialog(this,"Error","Error",JOptionPane.ERROR_MESSAGE);
                return null;
             }
      if(fitxer.isFile()) {
        BufferedWriter _buf = null;
        try {
          _buf = new BufferedWriter(new FileWriter(fitxer, true));
        } catch (Exception ex) {
          //Logger.getLogger(FileControllerD.class.getName()).log(Level.SEVERE, null, ex);
        }
          return _buf;
        }
      return null;
    }

     /**
    *
    *
    * Retorna un buffer d'escriptura del fitxer de text indicat per una ruta.
    *
    * @author oriol.cano
    * @param String path: Indica la ruta del fitxer de text.
    * @return BufferedWriter amb el contingut del fitxer de text.
    * @throws -
    */
    public BufferedWriter getFileW(String path) {
        File _file = new File(path);
    BufferedWriter _buf = null;
    try {
      _buf = new BufferedWriter(new FileWriter(_file,true));
    } catch (Exception ex) {
      //Logger.getLogger(FileControllerD.class.getName()).log(Level.SEVERE, null, ex);
    }
    return _buf;


    }
    /**
    *
    *
    * Guarda en un fitxer de text seleccionat mitjançant un JFileChooser
    * el contingut d'un JtextArea
    *
    * @author oriol.cano
    * @param JTextArea ta: JTextArea del que s'ha de guardar el contingut en un fitxer de text.
    * @return -
    * @throws -
    */
    public void saveFile(JTextArea ta) {
        FileControllerP fCP = new FileControllerP();
        _fc = fCP.chooseFile();
        File name = _fc.getSelectedFile();

        if(!name.exists()) {
            try {
                PrintWriter output = new PrintWriter(new FileWriter(name + ".txt",true));
                output.write(ta.getText());
                output.close();
            }
            catch (IOException ioException){
              JOptionPane.showMessageDialog(null, "Error en el archivo","Error",JOptionPane.ERROR_MESSAGE);
            }
        }
        else {
            int n = JOptionPane.showConfirmDialog(this,"El archivo ya existe desea reemplazarlo?");
            if (n == JOptionPane.YES_OPTION) {
                try {
                    PrintWriter output = new PrintWriter(new FileWriter(name));
                    output.write(ta.getText());
                    output.close();
                }
                catch (IOException ioException){
                    JOptionPane.showMessageDialog(null, "Error en el archivo","Error",JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }
}
TOP

Related Classes of data.FileControllerD

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.