Created
February 2, 2017 09:48
-
-
Save calvernaz/05994a731054ed223ed0a04f52e91c13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.util.filesystem; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.charset.Charset; | |
import java.nio.file.FileSystems; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.attribute.FileAttribute; | |
import java.nio.file.attribute.PosixFilePermission; | |
import java.nio.file.attribute.PosixFilePermissions; | |
import java.util.Collections; | |
import java.util.EnumSet; | |
import java.util.List; | |
import java.util.Set; | |
import org.apache.commons.io.IOUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public abstract class FileSystemUtilities { | |
private static final String EMPTY_STRING = ""; | |
private static final String OS = System.getProperty("os.name") | |
.toLowerCase(); | |
public static final Logger LOGGER = LoggerFactory.getLogger(FileSystemUtilities.class); | |
/** | |
* Check for file permissions. | |
* <p/> | |
* | |
* <pre> | |
* FileSystemUtilities.checkPermissions(filePath, EnumSet.of(FilePermission.READABLE, FilePermission.WRITEABLE)) | |
* </pre> | |
* | |
* @param file | |
* a {@link File} where permissions will be tested, | |
* @param permissions | |
* a set of {@link FilePermission} instances. | |
* @return true, if successful, false otherwise | |
*/ | |
public static boolean checkPermissions(File file, Set<FilePermission> permissions) { | |
for (FilePermission filePermission : permissions) | |
if (!filePermission.testPermission(file)) return false; | |
return true; | |
} | |
/** | |
* Copy one stream to another. | |
* | |
* @param copyFrom | |
* the stream to copy from. | |
* @param copyTo | |
* the stream to copy to. | |
* @return true, if successful, false otherwise | |
*/ | |
public static boolean copy(InputStream copyFrom, OutputStream copyTo) { | |
assert copyFrom != null && copyTo != null : "Illegal or inappropriate arguments"; | |
boolean success = false; | |
try { | |
IOUtils.copy(copyFrom, copyTo); | |
success = true; | |
} catch (IOException e) { | |
LOGGER.error("Error thrown while copying file ", e); | |
success = false; | |
} finally { | |
IOUtils.closeQuietly(copyTo); | |
IOUtils.closeQuietly(copyFrom); | |
} | |
return success; | |
} | |
/** | |
* @param fromPath | |
* the string representing the path where file can be found. | |
* @param withName | |
* the name of the file | |
* @return a list with all lines, empty lines will be included. If file doesn't exist, a empty collection will be | |
* return. | |
* @throws IOException | |
*/ | |
public static List<String> readFileWithLines(String fromPath, String withName) throws IOException { | |
Path path = FileSystems.getDefault() | |
.getPath(fromPath, withName); | |
if (!path.toFile() | |
.exists()) | |
return Collections.emptyList(); | |
return Files.readAllLines(path, Charset.defaultCharset()); | |
} | |
/** | |
* @param fromPath | |
* the string representing the path where file can be found. | |
* @param withName | |
* the name of the file | |
* @return a string with file content. If file doesn't exist, a empty collection will be return.svn commi | |
* @throws IOException | |
*/ | |
public static String readAsString(String fromPath, String withName) throws IOException { | |
Path path = FileSystems.getDefault() | |
.getPath(fromPath, withName); | |
if (!path.toFile() | |
.exists()) return EMPTY_STRING; | |
return new String(Files.readAllBytes(path), Charset.defaultCharset()); | |
} | |
/** | |
* | |
* | |
*/ | |
public static File createFile(String pathName, PosixFilePermission permission, PosixFilePermission... permissions) { | |
Path pathFile = Paths.get(pathName); | |
Path pathToFile; | |
try { | |
if (isWindows()) { | |
pathToFile = Files.createFile(pathFile); | |
} else { | |
pathToFile = Files.createFile(pathFile, setFilePermissions(EnumSet.of(permission, permissions))); | |
} | |
return pathToFile.toFile(); | |
} catch (IOException e) { | |
throw new RuntimeException("Unable to create file", e); | |
} | |
} | |
private static FileAttribute<Set<PosixFilePermission>> setFilePermissions(Set<PosixFilePermission> perms) { | |
return PosixFilePermissions.asFileAttribute(perms); | |
} | |
public static boolean isWindows() { | |
return (OS.contains("win")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment