com.espertech.esper.util.FileUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of esper Show documentation
Show all versions of esper Show documentation
Complex event processing and event series analysis component
/*
* *************************************************************************************
* Copyright (C) 2006-2015 EsperTech, Inc. All rights reserved. *
* http://www.espertech.com/esper *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
* *************************************************************************************
*/
package com.espertech.esper.util;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileUtil {
public static String findClasspathFile(String filename) {
URL url = FileUtil.class.getClassLoader().getResource(filename);
if (url != null) {
return url.getFile();
}
return null;
}
public static void findDeleteClasspathFile(String filename) {
URL url = FileUtil.class.getClassLoader().getResource(filename);
if (url != null) {
File file = new File(url.getFile());
file.delete();
}
}
public static String[] readClasspathTextFile(String filename) {
String filenameCp = findClasspathFile(filename);
if (filenameCp == null) {
throw new RuntimeException("Failed to find file '" + filename + "' in classpath");
}
List lines = new ArrayList();
try {
FileInputStream fis = new FileInputStream(filenameCp);
Scanner scanner = new Scanner(fis);
try {
while (scanner.hasNextLine()){
lines.add(scanner.nextLine());
}
}
finally{
scanner.close();
fis.close();
}
}
catch (IOException ex) {
throw new RuntimeException("Failed to read file '" + filename + "': " + ex.getMessage(), ex);
}
return lines.toArray(new String[lines.size()]);
}
public static String readTextFile(File file) throws IOException {
String newline = System.getProperty("line.separator");
FileReader fr = new FileReader(file);
try {
BufferedReader br = new BufferedReader(fr);
StringWriter buffer = new StringWriter();
String strLine;
while ((strLine = br.readLine()) != null) {
buffer.append(strLine);
buffer.append(newline);
}
return buffer.toString();
}
finally {
try {
fr.close();
} catch (IOException e) { }
}
}
public static List readFile(InputStream is)
{
InputStreamReader isr = new InputStreamReader(is);
try {
return readFile(isr);
}
finally {
try {
isr.close();
}
catch (IOException e) {
// fine
}
}
}
public static List readFile(Reader reader)
{
List list = new ArrayList();
BufferedReader bufferedReader = null;
try
{
bufferedReader = new BufferedReader(reader);
readFile(bufferedReader, list);
}
catch (FileNotFoundException e)
{
throw new RuntimeException("File not found: " + e.getMessage(), e);
}
catch (IOException e)
{
throw new RuntimeException("IO Error reading file: " + e.getMessage(), e);
}
finally
{
try
{
if (bufferedReader != null)
{
bufferedReader.close();
}
}
catch (IOException e)
{
}
}
return list;
}
public static String linesToText(List lines) {
StringWriter writer = new StringWriter();
for (String line : lines) {
writer.append(line)
.append(System.getProperty(
"line.separator"));
}
return writer.toString();
}
private static void readFile(BufferedReader reader, List list) throws IOException
{
String text;
// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
list.add(text);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy