results converting class created and method for pdf file prepared

This commit is contained in:
Marcin Hutek 2023-12-08 17:38:58 +01:00
parent bc5ea0bd76
commit 392460a794

View File

@ -0,0 +1,75 @@
package model;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConvertResult {
private String inputFile;
private String outputFile;
public ConvertResult(String inputFile, String outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile;
}
public void convertToPdf() {
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
document.open();
String line;
while ((line = br.readLine()) != null) {
document.add(new Paragraph(line));
}
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// public void convertToJson() {
// try {
// String txtData = new String(Files.readAllBytes(Paths.get(inputFile)));
// String[] lines = txtData.split("\n");
//
// String cityName = lines[0].substring("city: ".length()).trim();
// String description = lines[1].substring("description: ".length()).trim();
//// double temperature = Double.parseDouble(lines[2].substring("temperature: ".length()).trim()
//// .replace(",", "."));
// double temperature = Double.parseDouble(lines[2].replaceAll("[^\\d.,]", "").replace(",", "."));
// int pressure = Integer.parseInt(lines[3].substring("pressure: ".length()).trim().split(" ")[0]);
// int humidity = Integer.parseInt(lines[4].substring("humidity: ".length()).trim().split(" ")[0]);
//
// ObjectMapper objectMapper = new ObjectMapper();
// ArrayNode jsonArray = objectMapper.createArrayNode();
// ObjectNode cityNode = JsonNodeFactory.instance.objectNode();
//
// cityNode.put("name", cityName);
// cityNode.put("description", description);
// cityNode.put("temperature", temperature);
// cityNode.put("pressure", pressure);
// cityNode.put("humidity", humidity);
//
// jsonArray.add(cityNode);
//
// objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File(outputFile), jsonArray);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}