All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.util.FileHelper Maven / Gradle / Ivy

/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel 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 MetaModel.  If not, see .
 */
package dk.eobjects.metamodel.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Various helper methods for handling files
 */
public class FileHelper {

	private static final Log _log = LogFactory.getLog(FileHelper.class);

	public static final String ENCODING = "UTF-8";

	public static File getTempDir() {
		File result = null;
		String tmpDirPath = System.getenv("java.io.tmpdir");
		if (tmpDirPath != null && !"".equals(tmpDirPath)) {
			result = new File(tmpDirPath);
		} else {
			_log
					.warn("Could not determine tmpdir by using environment variable.");
			try {
				File file = File.createTempFile("foo", "bar");
				result = file.getParentFile();
				file.delete();
			} catch (IOException e) {
				_log.error(e);
				result = new File("metamodel.tmp.dir");
				result.mkdir();
				result.deleteOnExit();
			}
			_log.warn("Using '" + result.getAbsolutePath() + "' as tmpdir.");
		}
		return result;
	}

	public static BufferedWriter getBufferedWriter(File file) {
		return new BufferedWriter(getWriter(file));
	}

	public static Writer getWriter(File file) {
		try {
			return new OutputStreamWriter(new FileOutputStream(file), ENCODING);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void writeStringAsFile(File file, String string) {
		try {
			BufferedWriter bw = getBufferedWriter(file);
			bw.write(string);
			bw.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static BufferedReader getBufferedReader(File file) {
		return new BufferedReader(getReader(file));
	}

	public static Reader getReader(File file) {
		try {
			FileInputStream inputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(
					inputStream, ENCODING);
			return inputStreamReader;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String readFileAsString(File file) {
		try {
			BufferedReader br = getBufferedReader(file);
			StringBuilder sb = new StringBuilder();
			boolean firstLine = true;
			for (String line = br.readLine(); line != null; line = br
					.readLine()) {
				if (firstLine) {
					firstLine = false;
				} else {
					sb.append('\n');
				}
				sb.append(line);
			}
			br.close();
			return sb.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy