com.squeakysand.commons.io.FileHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squeakysand.commons.io;
import com.squeakysand.commons.lang.NullParameterException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* DOCUMENT ME!
*/
public final class FileHelper {
private static final Logger LOG = LoggerFactory.getLogger(FileHelper.class);
public static List findFilesByExtension(File searchDirectory, String extension) throws IOException {
IOFileFilter filter = new WildcardFileFilter("*." + extension);
FileFinder finder = new FileFinder(filter);
return finder.findFiles(searchDirectory);
}
/**
* DOCUMENT ME!
*
* @param filePath DOCUMENT ME!
* @return DOCUMENT ME!
* @throws java.io.IOException DOCUMENT ME!
*/
public static List readLines(String filePath) throws IOException {
FileReader reader = new FileReader(filePath);
BufferedReader in = new BufferedReader(reader);
List lines = new ArrayList();
String line = in.readLine();
while (line != null) {
lines.add(line);
line = in.readLine();
}
in.close();
return lines;
}
public static void writeFileToStream(File file, OutputStream outputStream) throws IOException {
if (file == null) {
throw new IllegalArgumentException("file cannot be null");
}
if (outputStream == null) {
throw new IllegalArgumentException("outputStream cannot be null");
}
FileInputStream inputStream = new FileInputStream(file);
StreamHelper.copyInputStreamToOutputStream(inputStream, outputStream);
}
public static void writeLines(String filePath, List lines) throws IOException {
FileWriter writer = new FileWriter(filePath);
BufferedWriter out = new BufferedWriter(writer);
for (String line : lines) {
out.write(line);
out.newLine();
}
out.close();
}
public static void writeStreamToFile(File file, InputStream inputStream) throws IOException {
if (file == null) {
throw new NullParameterException("file");
}
if (inputStream == null) {
throw new NullParameterException("inputStream");
}
FileOutputStream outputStream = new FileOutputStream(file);
StreamHelper.copyInputStreamToOutputStream(inputStream, outputStream);
}
private FileHelper() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy