FileUtils.java
package org.codehaus.gmavenplus.util;
import java.io.*;
/**
* A collection of file utility methods taken from Guava so we don't have to depend on Guava.
*
* @author Keegan Witt
*/
public class FileUtils {
private FileUtils() {
}
/**
* Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
*
* @param file the file to get the extension from
* @return the file extension
*/
public static String getFileExtension(final File file) {
String fileName = file.getName();
int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
}
/**
* Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
*
* @param file the file to get the extension from
* @return the file extension
*/
public static String getFileExtension(final String file) {
return getFileExtension(new File(file));
}
/**
* Returns the filename without the extension or '.'.
*
* @param file the file remove the extension from
* @return the file name without its path or extension
*/
public static String getNameWithoutExtension(final File file) {
String fileName = file.getName();
int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
}
/**
* Returns the filename without the extension or '.'.
*
* @param file the file remove the extension from
* @return the file name without its path or extension
*/
public static String getNameWithoutExtension(final String file) {
return getNameWithoutExtension(new File(file));
}
/**
* Closes the InputStream if it is not null, swallowing any exceptions.
*
* @param inputStream the InputStream to close
*/
public static void closeQuietly(final InputStream inputStream) {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// do nothing, close quietly
}
}
}
/**
* Closes the OutputStream if it is not null, swallowing any exceptions.
*
* @param outputStream the OutputStream to close
*/
public static void closeQuietly(final OutputStream outputStream) {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// do nothing, close quietly
}
}
}
/**
* Closes the Reader if it is not null, swallowing any exceptions.
*
* @param reader the Reader to close
*/
public static void closeQuietly(final Reader reader) {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// do nothing, close quietly
}
}
}
/**
* Closes the Writer if it is not null, swallowing any exceptions.
*
* @param writer the Writer to close
*/
public static void closeQuietly(final Writer writer) {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// do nothing, close quietly
}
}
}
}