package at.bakery.kippen.server.outlets; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; /** JDK 6 or before. */ public class CsvKippOutlet extends AbstractKippOutlet{ File aFile; /** * @param filename * is an existing file which can be written to. * @throws FileNotFoundException * if the file does not exist. * @throws IOException * if problem encountered during write. * */ public CsvKippOutlet(String path, String filename) throws IOException, FileNotFoundException { String pathFolder = System.getProperty(path); this.aFile = new File(pathFolder, filename); if (aFile == null) { throw new IllegalArgumentException("File should not be null."); } if (!aFile.exists()) { throw new FileNotFoundException("File does not exist: " + aFile); } if (!aFile.isFile()) { throw new IllegalArgumentException("Should not be a directory: " + aFile); } if (!aFile.canWrite()) { throw new IllegalArgumentException("File cannot be written: " + aFile); } } /** * Fetch the entire contents of a text file, and return it in a String. This * style of implementation does not throw Exceptions to the caller. * * @param aFile * is a file which already exists and can be read. */ public String getContents() { // ...checks on aFile are elided StringBuilder contents = new StringBuilder(); try { // use buffering, reading one line at a time // FileReader always assumes default encoding is OK! BufferedReader input = new BufferedReader(new FileReader(aFile)); try { String line = null; // not declared within while loop /* * readLine is a bit quirky : it returns the content of a line * MINUS the newline. it returns null only for the END of the * stream. it returns an empty String if two newlines appear in * a row. */ while ((line = input.readLine()) != null) { contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } return contents.toString(); } public void output(String aContent) throws IOException { Writer output = new BufferedWriter(new FileWriter(aFile)); try { // FileWriter always assumes default encoding is OK! output.write(aContent); } finally { output.close(); } } @Override public void output() { // TODO Auto-generated method stub } }