Hallo!
Ich versuche mich grade im kopieren,verschieben von Dateien und wollte dies nun auf ein von mir schon geschriebenes und funktionierendes Programm anwenden, jedoch habe keinen blassen Schimmer wo ich das .close() einsetzten muss.
(Ohne bekomme ich die Fehlermeldung, dass der File schon von einer Software geöffnet sei)
Ich versuche mich grade im kopieren,verschieben von Dateien und wollte dies nun auf ein von mir schon geschriebenes und funktionierendes Programm anwenden, jedoch habe keinen blassen Schimmer wo ich das .close() einsetzten muss.
(Ohne bekomme ich die Fehlermeldung, dass der File schon von einer Software geöffnet sei)
Java:
package PdfcopyPackage;
import java.io.File;
import java.io.FileNotFoundException;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.Watchable;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class Main {
public static void main2(String[] args){
File f = new File("C:/Users/Robin/Desktop/Marius-Projekt/");
File[] fileArray = f.listFiles();
String FileS = fileArray[0].toString();
String[] splitFileS = FileS.split("\\\\");
String[] splitFileS2 =splitFileS[5].split("\\.");
Var.PdfName = splitFileS2[0];
StringBuffer buff = new StringBuffer();
try {
PdfReader reader = new PdfReader(
FileS);
int numberOfPages = reader.getNumberOfPages();
String s;
for (int i = 1; i <= numberOfPages; i++) {
s = PdfTextExtractor.getTextFromPage(reader, i);
buff.append(s + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
String[] splitbuff = buff.toString().split("\n");
Var.KundenAnschrift1 = splitbuff[23];
Var.KundenAnschrift2 = splitbuff[24];
Var.InstallateurAnschrift1 = splitbuff[34];
Var.InstallateurAnschrift2 = splitbuff[35];
String[] splithelp = splitbuff[25].split(" ");
int i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Tel.")){
Var.KundenTel = Var.KundenTel+splithelp[i]+" ";
}
i++;
}
splithelp = splitbuff[21].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Seriennummer:")){
String[] splithelp2 = splithelp[1].split("");
int i3 = 0;
boolean help2 = true;
String help = "";
while ( help2 == true ){
if (!splithelp2[i3].equals("0") && !splithelp2[i3].equals("1") && !splithelp2[i3].equals("2") && !splithelp2[i3].equals("3") && !splithelp2[i3].equals("4") && !splithelp2[i3].equals("5") && !splithelp2[i3].equals("6") && !splithelp2[i3].equals("7") && !splithelp2[i3].equals("8") && !splithelp2[i3].equals("9") && !splithelp2[i3].equals("-")) {
help2 = false;
}
else if (help2 == true) {
Var.Seriennummer = Var.Seriennummer+splithelp2[i3];
}
i3++;
}
}
i++;
}
splithelp = splitbuff[20].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Produkt:") && !splithelp[i].equals(splithelp[1])){
Var.Produkt = Var.Produkt+splithelp[i]+" ";
}
i++;
}
splithelp = splitbuff[33].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Installateur:")){
Var.InstallateurName = Var.InstallateurName+splithelp[i]+" ";
}
i++;
}
splithelp = splitbuff[36].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Tel.")){
Var.InstallateurTel = Var.InstallateurTel+splithelp[i]+" ";
}
i++;
}
splithelp = splitbuff[27].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Mobil")){
Var.KundenMobil = Var.KundenMobil+splithelp[i]+" ";
}
i++;
}
splithelp = splitbuff[28].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Mail")){
Var.KundenMail = Var.KundenMail+splithelp[i]+" ";
}
i++;
}
splithelp = splitbuff[22].split(" ");
i = 0;
while (i != splithelp.length){
if (!splithelp[i].equals("Standort:")){
Var.KundenName = Var.KundenName+splithelp[i]+" ";
}
i++;
}
try {
createPdf("C:/Users/Robin/Desktop/Marius-Projekt/ZielOrdner/"+ Var.PdfName + "IBNProtokoll"+".pdf");
} catch (Exception e) {
e.printStackTrace();
}
try {
modifyPdf("C:/Users/Robin/Desktop/Marius-Projekt/Muster/IBNProtokollMuster.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createPdf(String filename) throws Exception{
FileOutputStream fos = new FileOutputStream(filename);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfPTable tabelleaussen = new PdfPTable(new float[]{109f});
tabelleaussen.setTotalWidth(500f);
tabelleaussen.writeSelectedRows(0, -1, 20, 700, writer.getDirectContent());
document.close();
fos.close();
}
public static void modifyPdf(String filename) throws Exception{
BaseFont baseFont = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED);
PdfReader reader = new PdfReader(filename);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/Users/Robin/Desktop/Marius-Projekt/ZielOrdner/"+Var.PdfName+"IBNProtokoll"+".pdf"));
PdfContentByte over = stamper.getOverContent(1);
over.beginText();
over.setFontAndSize(baseFont, 14);
over.setTextMatrix(420,675);
over.showText(Var.Seriennummer);
over.endText();
PdfContentByte over2 = stamper.getOverContent(1);
over2.beginText();
over2.setFontAndSize(baseFont, 12);
over2.setTextMatrix(121,580);
over2.showText(Var.KundenName);
over2.endText();
PdfContentByte over3 = stamper.getOverContent(1);
over3.beginText();
over3.setFontAndSize(baseFont, 11);
over3.setTextMatrix(121,569);
over3.showText(Var.KundenAnschrift1);
over3.endText();
PdfContentByte over4 = stamper.getOverContent(1);
over4.beginText();
over4.setFontAndSize(baseFont, 12);
over4.setTextMatrix(121,548);
over4.showText(Var.KundenTel);
over4.endText();
PdfContentByte over5 = stamper.getOverContent(1);
over5.beginText();
over5.setFontAndSize(baseFont, 8);
over5.setTextMatrix(121,525);
over5.showText(Var.InstallateurName);
over5.endText();
PdfContentByte over6 = stamper.getOverContent(1);
over6.beginText();
over6.setFontAndSize(baseFont, 11);
over6.setTextMatrix(121,515);
over6.showText(Var.InstallateurAnschrift1);
over6.endText();
PdfContentByte over7 = stamper.getOverContent(1);
over7.beginText();
over7.setFontAndSize(baseFont, 12);
over7.setTextMatrix(121,496);
over7.showText(Var.InstallateurTel);
over7.endText();
PdfContentByte over8 = stamper.getOverContent(1);
over8.beginText();
over8.setFontAndSize(baseFont, 14);
over8.setTextMatrix(121,675);
over8.showText(Var.Produkt);
over8.endText();
PdfContentByte over9 = stamper.getOverContent(1);
over9.beginText();
over9.setFontAndSize(baseFont, 12);
over9.setTextMatrix(388,580);
over9.showText(Var.KundenMail);
over9.endText();
PdfContentByte over10 = stamper.getOverContent(1);
over10.beginText();
over10.setFontAndSize(baseFont, 12);
over10.setTextMatrix(388,568);
over10.showText(Var.KundenMobil);
over10.endText();
PdfContentByte over11 = stamper.getOverContent(1);
over11.beginText();
over11.setFontAndSize(baseFont, 11);
over11.setTextMatrix(121,559);
over11.showText(Var.KundenAnschrift2);
over11.endText();
PdfContentByte over12 = stamper.getOverContent(1);
over12.beginText();
over12.setFontAndSize(baseFont, 11);
over12.setTextMatrix(121,507);
over12.showText(Var.InstallateurAnschrift2);
over12.endText();
stamper.close();
Path p1 = Paths.get("C:/Users/Robin/Desktop/Marius-Projekt/"+Var.PdfName+".pdf");
Path p2 = Paths.get("C:/Users/Robin/Desktop/Marius-Projekt/ZielOrdner/Bestellungen/"+Var.PdfName+".pdf");
Files.move( p1, p2, REPLACE_EXISTING);
Var.stop++;
Var.Produkt = "";
Var.KundenName = "";
Var.KundenTel = "";
Var.KundenMail = "";
Var.KundenMobil = "";
Var.Seriennummer = "";
Var.InstallateurName = "";
Var.InstallateurTel = "";
if (Var.stop < 6){
Main.main2(null);
}else{
Main.main(null);
}
}
public static <DirectoryWatchService> void main(String[] args) throws IOException, InterruptedException{
Path path = Paths.get("C:/Users/Robin/Desktop/Marius-Projekt");
WatchService watchService = path.getFileSystem().newWatchService();
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
WatchKey watchKey = null;
while (true) {
watchKey = watchService.poll(10, TimeUnit.MINUTES);
if(watchKey != null) {
watchKey.pollEvents().stream().forEach(event -> Main.main2(args));
}
watchKey.reset();
}
}
}