/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. * * $Id: FileHelper.java,v 1.2 2006/05/15 12:05:21 wlackner Exp $ */ package at.knowcenter.wag.egov.egiz.tools; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.log4j.Logger; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; /** * This class provides file reader and writer methods. All methods are static! * * @author wlackner */ public class FileHelper { /** * The logger definition. */ private static final Logger logger_ = ConfigLogger.getLogger(FileHelper.class); /** * This method reads a file by reading line by line. * * @param fileName the file to be read * @return the content string of the file */ public static String readFromFile(String fileName) { String file_string = null; logger_.trace("Looking for file: " + fileName); try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); logger_.trace("Reading file: " + fileName); String line = null; file_string = ""; while ((line = reader.readLine()) != null) { file_string += line; } reader.close(); logger_.debug("File successfully read: " + fileName); } catch (FileNotFoundException e) { logger_.debug("File not found: " + fileName); } catch (IOException e) { logger_.debug("Error reading file: " + fileName); } return file_string; } /** * This method reads a file by reading line by line. * * @param fileName the file to be read * @return the content string of the file */ public static String readFromInputStream(InputStream inputStream) { String file_string = null; if (inputStream == null) { return null; } try { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line = null; file_string = ""; while ((line = reader.readLine()) != null) { file_string += line; } reader.close(); } catch (IOException e) { logger_.debug("Error reading inputstream."); } return file_string; } /** * This method writes a file line by line. * * @param fileName the file to be written * @param fileString the content to be written * @return true if the file could be written sucessfully, false otherwise */ public static boolean writeToFile(String fileName, String fileString) { BufferedWriter writer; try { FileWriter fwriter = new FileWriter(fileName); writer = new BufferedWriter(fwriter); writer.write(fileString); writer.close(); } catch (IOException e) { logger_.info("File:" + fileName + " can not be written. Cause:" + e.getMessage()); return false; } return true; } }