Advertisement
Guest User

Dúvida

a guest
May 23rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. /**
  2.  *  le ficheiro de leituras globais  se o ficheiro já existir ele acrescenta se não ele cria
  3.  */
  4. public static void LeFicheiroLeituras()
  5. {
  6.     File inputFile = new File("Leituras.txt");
  7.     Scanner in = null;
  8.     try {
  9.         in = new Scanner(inputFile);
  10.         while(in.hasNextLine())
  11.         {
  12.        
  13.             String linha = in.nextLine();
  14.             String[] campos = SeparaCamposLeituras(linha);
  15.             File file = new File(campos[0]+".txt");
  16.             if(file.exists())
  17.             {
  18.               AcrescentarAoFicheiroData(campos);
  19.            
  20.             }
  21.             else
  22.             {
  23.               CriaFicheiroData(campos);
  24.             }
  25.         }  
  26.     } catch (FileNotFoundException e) {
  27.         // TODO Auto-generated catch block
  28.         System.out.print("problema no carregar Utilizadores");
  29.     }
  30.     finally
  31.     {
  32.         if(in != null)
  33.         {
  34.             in.close();
  35.         }
  36.     }
  37.  }
  38.  
  39. /**
  40.  * Cria ficheiro com a respetiva data
  41.  * @param campos linhas ficheiro leituras
  42.  */
  43. public static void CriaFicheiroData(String []campos)
  44. {
  45.     PrintWriter out = null;
  46.     try {
  47.        
  48.         out = new PrintWriter(campos[0] + ".txt");
  49.         String linha = campos[1] + "==" + campos[2] + "==" + campos[3];
  50.         out.println(linha);
  51.        
  52.     } catch (FileNotFoundException e) {
  53.         // TODO Auto-generated catch block
  54.         e.printStackTrace();
  55.     }
  56.     finally
  57.     {
  58.         if(out!=null)
  59.         {
  60.             out.close();
  61.         }
  62.     }
  63. }
  64.  
  65. /**
  66.  * Acrescenta ao ficheiro as datas correspondentes
  67.  * @param campos linhas ficheiro leituras
  68.  */
  69. public static void AcrescentarAoFicheiroData(String []campos)
  70. {
  71.     File newFile = new File(campos[0] + ".txt");
  72.     PrintWriter out = null;
  73.     try {
  74.         FileOutputStream fos = new FileOutputStream(newFile,true);
  75.         out = new PrintWriter(fos);
  76.         String linha = campos[1] + "==" + campos[2] + "==" + campos[3];
  77.         out.println(linha);
  78.     } catch (FileNotFoundException e) {
  79.         e.printStackTrace();
  80.     }
  81.     finally
  82.     {
  83.         if(out!=null)
  84.         {
  85.             out.close();
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. public static String[] SeparaCamposLeituras(String linha)
  92. {
  93.   final int TAM = 4;
  94.  
  95.   String data = "";
  96.   String hora = "";
  97.   String antena = "";
  98.   String etiqueta = "";
  99.  
  100.    for(int i = 0;i<linha.length();i++)
  101.      {
  102.        if(i <= 9)
  103.        {
  104.            data = data + linha.charAt(i);
  105.        }
  106.        if(i>9 && i<=17)
  107.        {
  108.            hora = hora + linha.charAt(i);
  109.        }
  110.        if(i>17 && i<=20)
  111.        {
  112.            antena = antena + linha.charAt(i);
  113.        }
  114.        if(i>20 && i<=24)
  115.        {
  116.            etiqueta = etiqueta + linha.charAt(i);
  117.        }
  118.        
  119.      }
  120.    
  121.    String[] campos = new String[4];
  122.    campos[0] = data;
  123.    campos[1] = hora;
  124.    campos[2] = antena;
  125.    campos[3] = etiqueta;
  126.    
  127.    return campos;
  128.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement