Eclipseplugins
ContentWriter.java
1 package com.proalpha.pds.generator;
2 
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 
9 import org.apache.commons.io.IOUtils;
10 
18 class ContentWriter {
19 
20  private final File outputFile;
21  private final String content;
22 
29  public ContentWriter(File outputFile, String content) {
30 
31  this.outputFile = outputFile;
32  this.content = content;
33  }
34 
41  public void writeContentToHarddisk() throws IOException {
42 
43  createFileOnHarddisk();
44 
45  PrintWriter printWriter = null;
46 
47  try {
48 
49  printWriter = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
50 
51  printWriter.print(content);
52 
53  } finally {
54 
55  IOUtils.closeQuietly(printWriter);
56 
57  }
58  }
59 
60  private void createFileOnHarddisk() throws IOException {
61 
62  if (outputFile == null) {
63  throw new IOException("Output File is not set.");
64  }
65 
66  Boolean fileExits = outputFile.exists();
67 
68  if (!fileExits) {
69  outputFile.getParentFile().mkdirs();
70  fileExits = outputFile.createNewFile();
71  }
72 
73  if (!fileExits) {
74  throw new IOException("Could not create " + outputFile);
75  }
76  }
77 
78 }