1 - Crie um programa em Java que escreva a seguinte mensagem em um arquivo chamado "arquivo.txt": "Conteúdo a ser gravado no arquivo." Utilize as classes do pacote java.io.
package Challenges04.exercise01;
import java.io.*;
public class Service {
public void run() throws IOException, RuntimeException {
var file = new FileWriter("Archive.txt");
file.write("Content to be written to the file");
file.close();
}
}
package Challenges04.exercise01;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
var test = new Service();
try {
test.run();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Saída esperada:
Archive.txt > Content to be written to the file
2 - Defina uma classe chamada Titulo com os atributos necessários. Em seguida, crie um programa que instancia um objeto Titulo, serializa esse objeto para JSON usando a biblioteca Gson e imprime o resultado.
&
3 - Modifique o programa anterior para que o JSON gerado seja formatado de maneira mais elegante. Utilize o método setPrettyPrinting para alcançar esse resultado.
package Challenges04.exercise02and03;
import com.google.gson.*;
public class Title {
//variables
private String title;
private int releaseDate;
private double duration;
private String genders;
private double rating;
private String director;
//contructor
public Title(String title, int releaseDate, double duration, String genders, double rating, String director) {
this.title = title;
this.releaseDate = releaseDate;
this.duration = duration;
this.genders = genders;
this.rating = rating;
this.director = director;
}
//getters
public String getTitle() {
return title;
}
public int getReleaseDate() {
return releaseDate;
}
public double getDuration() {
return duration;
}
public String getGenders() {
return genders;
}
public double getRating() {
return rating;
}
public String getDirector() {
return director;
}
public void serialize() {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(Title.class, new TitleSerializer())
.create();
String json = gson.toJson(this);
System.out.println(json);
}
}
package Challenges04.exercise02and03;
import com.google.gson.*;
import java.lang.reflect.Type;
public class TitleSerializer implements JsonSerializer<Title>{
@Override
public JsonElement serialize(Title src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("Title" , src.getTitle());
jsonObject.addProperty("Release Date", src.getReleaseDate());
jsonObject.addProperty("Duration", src.getDuration()+ " min");
jsonObject.addProperty("Genders", src.getGenders());
jsonObject.addProperty("Rating", src.getRating());
jsonObject.addProperty("Director", src.getDirector());
return jsonObject;
}
}
package Challenges04.exercise02and03;
public class Main {
public static void main(String[] args) {
var test1 = new Title("The Godfather", 1972, 175,
"Crime, Drama", 9.2, "Francis Ford Coppola");
var teste2 = new Title("The Conjuring", 2013, 112,
"terror", 7.5, "James Wan");
test1.serialize();
teste2.serialize();
}
}
Saída esperada:
{
"Title": "The Godfather",
"Release Date": 1972,
"Duration": "175.0 min",
"Genders": "Crime, Drama",
"Rating": 9.2,
"Director": "Francis Ford Coppola"
}
{
"Title": "The Conjuring",
"Release Date": 2013,
"Duration": "112.0 min",
"Genders": "terror",
"Rating": 7.5,
"Director": "James Wan"
}