Java File cookbook

By Maurizio Farina | Posted on September 2017

Maven dependencies

1
2
3
4
5
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.4</version>
</dependency>

How to create a file from a POJO?

1
2
3
4
5
private void flush2JsonFile() throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
    writer.writeValue(new File(filePath), report);
}

How to read a JSON from a file?

1
2
3
4
File file = new File("jsonFile.json");
byte[] jsonData = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
ObjectMapper objectMapper = new ObjectMapper();
Issue issue = objectMapper.readValue(jsonData, Issue.class);

How to serialize Date Time?

The library includes annotations for Date attributes. To avoid to annotate all Date attributes used by classes to serialize as Json is possible to specify the format directly to the Mapper:

1
2
3
4
5
6
7
8
9
private void flush2JsonFile() throws JsonGenerationException, JsonMappingException, IOException {

    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(df);
    ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
    writer.writeValue(new File(filePath), report);
}

Annotations useful for Marshalling

 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.listfeeds.report;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;

// Ignore all null properties
@JsonInclude(Include.NON_EMPTY)

// Ignore properties declararion at class level
@JsonIgnoreProperties(value = { "helper" })
public class BaseReport {

    String label;
    Date started;
    Date ended;

    // Rename property
    @JsonProperty("reports")
    List<BaseReport> baseReports;

    List<String> errors;

    // Ignore property at attribute level
    @JsonIgnore
    ReportHelper helper;

    public BaseReport(String label, Date dtStarted) {

        this.setLabel(label);
        this.setStarted(dtStarted);
    }

    public Date getStarted() {
        return started;
    }
    public void setStarted(Date started) {
        this.started = started;
    }
    public Date getEnded() {
        return ended;
    }
    public void setEnded(Date ended) {
        this.ended = ended;
    }

    public void end() {
        this.ended = new Date();
    }

    public List<BaseReport> getBaseReports() {
        return baseReports;
    }

    public void setBaseReports(List<BaseReport> baseReports) {
        this.baseReports = baseReports;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String report) {
        this.label = report;
    }


    public ReportHelper getHelper() {
        return helper;
    }

    public void setHelper(ReportHelper helper) {
        this.helper = helper;
    }

    public List<String> getErrors() {
        return errors;
    }

    public void setErrors(List<String> errors) {
        this.errors = errors;
    }
}