Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
FVHTTP Server - Impostazioni.java

Impostazioni.java

Caricato da: Fraioveio
Scarica il programma completo

  1. package fvhttpserver;
  2.  
  3. import java.io.*;
  4.  
  5. public class Impostazioni {
  6.     public int port;
  7.     public String basedir;
  8.    
  9.     private Impostazioni(int p, String b) {
  10.         port = p;
  11.         basedir = b;
  12.     }
  13.    
  14.     public static Impostazioni getImpostazioni() {
  15.         File f = new File("config.ini");
  16.         if(!f.exists()) {
  17.             try {
  18.                 createFile();
  19.             } catch (IOException ex) {}
  20.         }
  21.         try {
  22.             DataInputStream dis = new DataInputStream(new FileInputStream(f));
  23.            
  24.             String tmp = "";
  25.             int p = 80;
  26.             String b = "";
  27.            
  28.             while((tmp = dis.readLine()) != null) {
  29.                 if(tmp.length()>= 5 && tmp.toLowerCase().substring(0, 5).equals("port:")) {
  30.                     p = Integer.parseInt(tmp.substring(5));
  31.                 }
  32.                 if(tmp.length()>= 8 && tmp.toLowerCase().substring(0, 8).equals("basedir:")) {
  33.                     b = tmp.substring(8);
  34.                 }
  35.             }
  36.            
  37.             dis.close();
  38.            
  39.             return new Impostazioni(p, b);
  40.         } catch (IOException ex) {
  41.             System.out.println("Impossibile leggere nel file di configurazione!!\n    " + ex.toString());
  42.             try {
  43.                 createFile();
  44.             } catch (IOException ex1) {
  45.                 System.out.println("Impossibile creare ll file di configurazione!!\n    " + ex.toString());
  46.             }
  47.             return new Impostazioni(80, "");
  48.         }
  49.     }
  50.    
  51.     public static void setImpostazioni(int port, String basedir) throws IOException {
  52.         File f = new File("config.ini");
  53.         f.delete();
  54.         f.createNewFile();
  55.        
  56.         PrintStream out = new PrintStream(new FileOutputStream(f));
  57.        
  58.         out.print("port:" + port + "\n"
  59.                 + "basedir:" + basedir);
  60.        
  61.         out.close();
  62.        
  63.         Main.basedir = basedir;
  64.     }
  65.    
  66.     private static void createFile() throws IOException {
  67.         File f = new File("config.ini");
  68.         f.delete();
  69.         f.createNewFile();
  70.        
  71.         PrintStream out = new PrintStream(new FileOutputStream(f));
  72.        
  73.         out.print("port:80\n"
  74.                 + "basedir:");
  75.        
  76.         out.close();
  77.     }
  78. }