JNotepad - FileManager.java
Cerca
 











FileManager.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package jnotepad;
  6.  
  7. import java.io.DataOutputStream;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.RandomAccessFile;
  13.  
  14. /**
  15.  *
  16.  * @author paolo
  17.  */
  18. public class FileManager {
  19.  
  20.     FileOutputStream out;
  21.     DataOutputStream outd;
  22.     File f;
  23.  
  24.     /**
  25.      * Ritorna una istanza non funzionante di Filemanager. Per rendere operativa<br>
  26.      * questa istanza bisogna settarne il file;
  27.      */
  28.     public FileManager(){
  29.         this.f=null;
  30.         this.io=null;
  31.         this.out=null;
  32.         this.outd=null;
  33.     }
  34.  
  35.     public void setFile(File pFile) {
  36.         if(this.f!=null){
  37.             this.close();
  38.         }
  39.         this.f= pFile;
  40.         if (!this.f.exists()) {
  41.             try {
  42.                 this.f.createNewFile();
  43.             } catch (IOException ex) {
  44.             }
  45.         }
  46.         try {
  47.             this.io = new RandomAccessFile(this.f, "rw");
  48.             this.out = new FileOutputStream(this.f, true);
  49.             this.outd = new DataOutputStream(this.out);
  50.         } catch (FileNotFoundException e) {
  51.             System.err.println(e);
  52.         }
  53.     }
  54.  
  55.  
  56.     /**
  57.      * Crea una nuova instanza del file, apre la connessione col file specificato nel parametro. N.B. se il file non esiste viene creato.
  58.      * @param pPathFile il path del file con cui si vuole stabilire una connessione.
  59.      */
  60.     public FileManager(String pPathFile) {
  61.         this(new File(pPathFile));
  62.     }
  63.  
  64.     /**
  65.      * Crea una nuova instanza del file, apre la connessione col file specificato nel parametro. N.B. se il file non esiste viene creato.
  66.      * @param pFile Il file col quale si vuole aprire una connessione.
  67.      */
  68.     public FileManager(File pFile) {
  69.         this.f= pFile;
  70.         if (!this.f.exists()) {
  71.             try {
  72.                 this.f.createNewFile();
  73.             } catch (IOException ex) {
  74.             }
  75.         }
  76.         try {
  77.             this.io = new RandomAccessFile(this.f, "rw");
  78.             this.out = new FileOutputStream(this.f, true);
  79.             this.outd = new DataOutputStream(this.out);
  80.         } catch (FileNotFoundException e) {
  81.             System.err.println(e);
  82.         }
  83.     }
  84.  
  85.  
  86.     /**
  87.      * Scrive sul file con cui si è aperta la connessione. N.B. questa funzione sovrascrive il file.
  88.      * @param pStringa la stringa che si vuole scrivere sul file.
  89.      */
  90.     public void scrivi(String pStringa) {
  91.         this.cancella();
  92.         char[] stringa = pStringa.toCharArray();
  93.         for (int i = 0; i < stringa.length; i++) {
  94.             try {
  95.                 this.outd.write(stringa[i]);
  96.             } catch (IOException ex) {
  97.             }
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Chiude la connessione con il file.
  103.      */
  104.     public void close() {
  105.         try {
  106.             this.io.close();
  107.             this.out.close();
  108.             this.outd.close();
  109.         } catch (IOException ex) {
  110.         }
  111.     }
  112.  
  113.  
  114.     /**
  115.      * Cancella il contenuto del file con cui è stata stabita una connessione
  116.      */
  117.     public void cancella() {
  118.         this.elimina();
  119.         try {
  120.             this.f.createNewFile();
  121.         } catch (IOException ex) {
  122.         }
  123.         try {
  124.             this.io = new RandomAccessFile(this.f, "rw");
  125.             this.out = new FileOutputStream(this.f, true);
  126.             this.outd = new DataOutputStream(this.out);
  127.         } catch (FileNotFoundException e) {
  128.             System.err.println(e);
  129.         }
  130.  
  131.     }
  132.  
  133.     /**
  134.      * Elimina il file con cui è stata stabilita la connessione.
  135.      */
  136.     public void elimina() {
  137.         this.close();
  138.         this.f.delete();
  139.     }
  140.  
  141.     /**
  142.      * Aggiunge una stringa al file con cui si è stabilita la connessione.
  143.      * @param pStringa la stringa da aggiungere al file.
  144.      */
  145.     public void aggiungi(String pStringa) {
  146.         char[] str = pStringa.toCharArray();
  147.         for (int i = 0; i < str.length; i++) {
  148.             try {
  149.                 this.outd.write(str[i]);
  150.             } catch (IOException e) {
  151.             }
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * Legge il contenuto del file e lo salva in una stringa.
  157.      * @return Una stringa contenente il contenuto del file con cui si è aperta una connessione.
  158.      */
  159.     public String leggi() {
  160.         long lunghezza = this.f.length();
  161.         String retval = "";
  162.         for (int i = 0; i < lunghezza; i++) {
  163.             char c = 0;
  164.             try {
  165.                 this.io.seek(i);
  166.             } catch (IOException e) {
  167.             }
  168.             try {
  169.                 c = (char) this.io.readByte();
  170.             } catch (IOException ex) {
  171.             }
  172.             retval = retval + c;
  173.         }
  174.         return retval;
  175.     }
  176.  
  177.     /**
  178.      * Legge il contenuto del file e lo suddivide in parti ad ogni occorrenza di pSeparator trovata.
  179.      * @param pSeparator Una stringa da usare come separatore.
  180.      * @return Un array di stringhe contenente tutte stringhe comprese tra un'occorrenza e l'altra di pSeparator
  181.      */
  182.     public String[] leggi(String pSeparator) {
  183.         String str = this.leggi();
  184.         String[] retval = str.split(pSeparator);
  185.         return retval;
  186.     }
  187.  
  188.     /**
  189.      * Ritorna una specifica stringa tra due occorrenze.
  190.      * Esempio:
  191.      * Questo&è&un&esempio
  192.      * Con pSeparator & e pId 2 ritornerà  la stringa "un"
  193.      * @param pSeparator Una stringa da usare come separatore
  194.      * @param pId L'indice dell'occorrenza.
  195.      * @return Una stringa contenente una determinata stringa tra due occorrenze.
  196.      */
  197.     public String leggi(String pSeparator, int pId) {
  198.         String[] str = this.leggi(pSeparator);
  199.         if (pId < str.length) {
  200.             return str[pId];
  201.         }
  202.         return "";
  203.     }
  204.  
  205.     /**
  206.      * Copia il file con cui è stata aperta una connessione in un nuovo file;
  207.      *
  208.      * N.B. Se il Path non esiste crea un nuovo file con quel nome.
  209.      * N.B. Il file di origine non viene cancellato.
  210.      * @param pPath Il percorso del file in cui copiare.
  211.      */
  212.     public void copia(String pPath) {
  213.         FileManager file = new FileManager(pPath);
  214.         String thisfile = this.leggi();
  215.         file.scrivi(thisfile);
  216.     }
  217.  
  218.     public static void main(String... args){
  219.         FileManager f= new FileManager();
  220.         f.setFile(new File("C://Users/Paolo/Desktop/ciao.txt"));
  221.        // System.out.println(f.leggi());
  222.         f.setFile(new File("C://Users/Paolo/Desktop/ciao.txt"));
  223.         f.scrivi("ciao ciao");
  224.     }
  225.  
  226.  
  227. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.