public void write(Day day) {
        
        renameFile(day.getDate());
        File file = getFile(day.getDate());
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file);
            JSONWriter output = new JSONWriter(fileWriter);
            output.array();
            for (int i = 0; i < day.getCount(); i++) {
                Item item = day.getItem(i);
                output.object();
                output.key("name").value(item.getName());
                output.key("durationMinutes").value(item.getDurationMinutes());
                output.key("beginSet").value(item.isBeginSet());
                if (item.isBeginSet()) {
                    output.key("beginMinute").value(item.getBeginMinute());
                }
                output.endObject();
            }
            output.endArray();                
            fileWriter.close();
        } catch (FileNotFoundException ex) {
            try {
                throw new Error("Can't write to directory "+dataDirectory.getCanonicalPath(), ex);
            } catch (IOException ex1) {
                throw new Error("unexpected exception", ex);
            }
        } catch (IOException ex) {
            throw new Error("unexpected exception", ex);
        } catch (JSONException ex) {
            throw new Error("unexpected exception", ex);
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException ex) {
                    throw new Error("unexpected exception", ex);
                }
            }
        }
    }