//Ex: Criar um arquivo.txt
package challenges.part10;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFile {
public static void main(String[] args) throws IOException {
FileWriter file = new FileWriter("C:/alura/watchplus/src/challenges/part10/file.txt");
file.write("Content to be written to the file.");
file.close();
System.out.println("Programming finished correctly!");
}
}
//Ex: Classe Titulo
package challenges.part10;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
public class Title {
private String name;
private int yearOfRelease;
private String genre;
public Title(String name, int yearOfRelease, String genre) {
this.name = name;
this.yearOfRelease = yearOfRelease;
this.genre = genre;
}
public static class Main {
public static void main(String[] args) throws IOException {
Title myTitle = new Title("Armageddon", 1998, "Action");
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.create();
FileWriter writer = new FileWriter("C:/alura/watchplus/src/challenges/part10/title.json");
gson.toJson(myTitle, writer);
writer.close();
System.out.println("Programming finished correctly!");
}
}
}
//Ex: Classe Veiculo
package challenges.part10;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
public class Vehicle {
private String brand;
private String model;
private int manufactureYear;
private double price;
public Vehicle(String brand, String model, int manufactureYear, double price) {
this.brand = brand;
this.model = model;
this.manufactureYear = manufactureYear;
this.price = price;
}
public static void main(String[] args) throws IOException {
Vehicle myVehicle = new Vehicle("Tesla", "Model 3", 2023, 39.990);
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.create();
FileWriter writer = new FileWriter("C:/alura/watchplus/src/challenges/part10/vehicle.json");
gson.toJson(myVehicle, writer);
writer.close();
System.out.println("Programming finished correctly!");
}
}