/* GeneratorUtils.java * * Copyright 2010-2011 Johannes Kepler Universit�t Linz, * Institut f�r Wissensbasierte Mathematische Systeme. * * This file is part of pureImage. * * pureImage is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 3, * as published by the Free Software Foundation. * * pureImage is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with pureImage. If not, see <http://www.gnu.org/licenses/>. */ package pI.generator; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class GeneratorUtils { public static void createSourceFile(String outFile, String srcCode) { createSourceFile(outFile, srcCode, false); } public static void createSourceFile(String outFile, String srcCode, boolean append) { PrintWriter writer = null; try { File file = new File(outFile); File parentDir = file.getParentFile(); if(parentDir != null) { parentDir.mkdirs(); } writer = new PrintWriter(file); writer.write(srcCode); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (writer != null) writer.close(); } } public static String readFile(String file) throws IOException { int len = (int) (new File(file)).length(); FileInputStream fis = new FileInputStream(file); byte buf[] = new byte[len]; fis.read(buf); fis.close(); return new String(buf); } public static void writeFile(String file, String content) throws IOException { File _file = new File(file); _file.getParentFile().mkdirs(); FileWriter writer = new FileWriter(_file); writer.append(content); writer.flush(); writer.close(); } }