Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * le ficheiro de leituras globais se o ficheiro já existir ele acrescenta se não ele cria
- */
- public static void LeFicheiroLeituras()
- {
- File inputFile = new File("Leituras.txt");
- Scanner in = null;
- try {
- in = new Scanner(inputFile);
- while(in.hasNextLine())
- {
- String linha = in.nextLine();
- String[] campos = SeparaCamposLeituras(linha);
- File file = new File(campos[0]+".txt");
- if(file.exists())
- {
- AcrescentarAoFicheiroData(campos);
- }
- else
- {
- CriaFicheiroData(campos);
- }
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- System.out.print("problema no carregar Utilizadores");
- }
- finally
- {
- if(in != null)
- {
- in.close();
- }
- }
- }
- /**
- * Cria ficheiro com a respetiva data
- * @param campos linhas ficheiro leituras
- */
- public static void CriaFicheiroData(String []campos)
- {
- PrintWriter out = null;
- try {
- out = new PrintWriter(campos[0] + ".txt");
- String linha = campos[1] + "==" + campos[2] + "==" + campos[3];
- out.println(linha);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally
- {
- if(out!=null)
- {
- out.close();
- }
- }
- }
- /**
- * Acrescenta ao ficheiro as datas correspondentes
- * @param campos linhas ficheiro leituras
- */
- public static void AcrescentarAoFicheiroData(String []campos)
- {
- File newFile = new File(campos[0] + ".txt");
- PrintWriter out = null;
- try {
- FileOutputStream fos = new FileOutputStream(newFile,true);
- out = new PrintWriter(fos);
- String linha = campos[1] + "==" + campos[2] + "==" + campos[3];
- out.println(linha);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- finally
- {
- if(out!=null)
- {
- out.close();
- }
- }
- }
- public static String[] SeparaCamposLeituras(String linha)
- {
- final int TAM = 4;
- String data = "";
- String hora = "";
- String antena = "";
- String etiqueta = "";
- for(int i = 0;i<linha.length();i++)
- {
- if(i <= 9)
- {
- data = data + linha.charAt(i);
- }
- if(i>9 && i<=17)
- {
- hora = hora + linha.charAt(i);
- }
- if(i>17 && i<=20)
- {
- antena = antena + linha.charAt(i);
- }
- if(i>20 && i<=24)
- {
- etiqueta = etiqueta + linha.charAt(i);
- }
- }
- String[] campos = new String[4];
- campos[0] = data;
- campos[1] = hora;
- campos[2] = antena;
- campos[3] = etiqueta;
- return campos;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement