-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ficheiros.java
59 lines (47 loc) · 1.36 KB
/
Ficheiros.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package SEGURADORA;
/**
* Classe que grava e le de ficheiros.
*
* @author (RND)
* @version (2010)
*/
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class Ficheiros {
/** Escrever em ficheiro de texto */
public void outText(String fn) throws IOException {
PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(fn)));
pr.println(this.toString());
pr.close();
}
/** Ler de ficheiro de texto */
public static SGC inText(String fn) throws IOException, SGCException {
SGC res = new SGC();
BufferedReader br = new BufferedReader(new FileReader(fn));
StringTokenizer st, staux;
String s = br.readLine();
Comprador c;
if (!s.equals("SGC:"))
throw new SGCException("Ficheiro "+fn+"[erro]!");
s = br.readLine();
res.setTotalGasto(Float.valueOf(s).doubleValue());
s = br.readLine();
res.setPreço(Float.valueOf(s).doubleValue());
s = br.readLine();
st = new StringTokenizer(s, "{,=}");
br.close();
while (st.hasMoreTokens()) {
st.nextToken(); // avançar chave
try {
c = Comprador.valueOf(st.nextToken());
res.registaComprador(c);
}
catch (NoSuchMethodException e) {}
catch (IllegalAccessException e) {}
catch (ClassNotFoundException e) {}
catch (InvocationTargetException e) {}
}
return res;
}
}