Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void saveTestResult(int nif, boolean result) {
- JSONParser parser = new JSONParser();
- try {
- File file = new File("data/tests.json");
- JSONArray tests = new JSONArray();
- if (file.length() != 0) {
- Object obj = parser.parse(new FileReader(file));
- JSONObject jsonObject = (JSONObject) obj;
- tests = (JSONArray) jsonObject.get("test");
- }
- JSONObject test = new JSONObject();
- Date date = new Date();
- SimpleDateFormat formattedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
- test.put("nif", nif);
- test.put("result", result);
- test.put("date", formattedDate.format(date));
- tests.add(test);
- JSONObject allTests = new JSONObject();
- allTests.put("test", tests);
- try ( FileWriter fileWriter = new FileWriter("data/tests.json")) {
- fileWriter.write(allTests.toJSONString());
- } catch (Exception e) {
- }
- } catch (FileNotFoundException ex) {
- System.out.println("Ficheiro não encontrado");
- } catch (IOException e) {
- System.out.println("Não existem");
- } catch (ParseException ex) {
- System.out.println("Não foi possível validar o ficheiro de utilizadores");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement