Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
get Path modified time
  • Loading branch information
rahmanusta committed Sep 12, 2015
1 parent 7a9afec commit cf1522a
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/main/java/com/kodcu/other/IOHelper.java
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
Expand All @@ -39,10 +40,10 @@ public static void writeToFile(File file, String content, StandardOpenOption...
writeToFile(file.toPath(), content, openOption); writeToFile(file.toPath(), content, openOption);
} }


public static Optional<IOException> writeToFile(Path path, String content, StandardOpenOption... openOption) { public static Optional<Exception> writeToFile(Path path, String content, StandardOpenOption... openOption) {
try { try {
Files.write(path, content.getBytes(Charset.forName("UTF-8")), openOption); Files.write(path, content.getBytes(Charset.forName("UTF-8")), openOption);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while writing to {}", path, e); logger.error("Problem occured while writing to {}", path, e);
return Optional.of(e); return Optional.of(e);
} }
Expand All @@ -52,7 +53,7 @@ public static Optional<IOException> writeToFile(Path path, String content, Stand
public static void writeToFile(Path path, byte[] content, StandardOpenOption... openOption) { public static void writeToFile(Path path, byte[] content, StandardOpenOption... openOption) {
try { try {
Files.write(path, content, openOption); Files.write(path, content, openOption);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while writing {}", path, e); logger.error("Problem occured while writing {}", path, e);
} }
} }
Expand All @@ -62,17 +63,17 @@ public static String readFile(InputStream inputStream) {
try { try {
content = IOUtils.toString(inputStream, "UTF-8"); content = IOUtils.toString(inputStream, "UTF-8");
IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(inputStream);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while reading inputstream", e); logger.error("Problem occured while reading inputstream", e);
} }
return content; return content;
} }


public static String readFile(Path path) { public static String readFile(Path path) {
String content = ""; String content = "";
try (InputStream is = Files.newInputStream(path, StandardOpenOption.READ)) { try (InputStream is = Files.newInputStream(path, StandardOpenOption.READ, StandardOpenOption.SYNC)) {
content = IOUtils.toString(is, "UTF-8"); content = IOUtils.toString(is, "UTF-8");
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while reading file {}", path, e); logger.error("Problem occured while reading file {}", path, e);
} }
return content; return content;
Expand All @@ -81,7 +82,7 @@ public static String readFile(Path path) {
public static void createDirectories(Path path) { public static void createDirectories(Path path) {
try { try {
Files.createDirectories(path); Files.createDirectories(path);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while creating directories {}", path, e); logger.error("Problem occured while creating directories {}", path, e);
} }


Expand All @@ -90,7 +91,7 @@ public static void createDirectories(Path path) {
public static Path createTempFile(String suffix) { public static Path createTempFile(String suffix) {
try { try {
return Files.createTempFile("asciidoc-temp", suffix); return Files.createTempFile("asciidoc-temp", suffix);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while creating temp file", e); logger.error("Problem occured while creating temp file", e);
} }


Expand All @@ -103,7 +104,7 @@ public static Path createTempFile(Path path, String suffix) {
} }
try { try {
return Files.createTempFile(path, "asciidoc-temp", suffix); return Files.createTempFile(path, "asciidoc-temp", suffix);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while creating temp file {}", path, e); logger.error("Problem occured while creating temp file {}", path, e);
} }


Expand All @@ -113,15 +114,15 @@ public static Path createTempFile(Path path, String suffix) {
public static void copy(Path source, Path target, CopyOption... copyOptions) { public static void copy(Path source, Path target, CopyOption... copyOptions) {
try { try {
Files.copy(source, target, copyOptions); Files.copy(source, target, copyOptions);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while copying {} to {}", source, target, e); logger.error("Problem occured while copying {} to {}", source, target, e);
} }
} }


public static String pathToUrl(Path path) { public static String pathToUrl(Path path) {
try { try {
return path.toUri().toURL().toString(); return path.toUri().toURL().toString();
} catch (MalformedURLException e) { } catch (Exception e) {
logger.error("Problem occured while getting URL of {}", path, e); logger.error("Problem occured while getting URL of {}", path, e);
} }
return null; return null;
Expand All @@ -130,7 +131,7 @@ public static String pathToUrl(Path path) {
public static Stream<Path> list(Path path) { public static Stream<Path> list(Path path) {
try { try {
return Files.list(path); return Files.list(path);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while listing {}", path, e); logger.error("Problem occured while listing {}", path, e);
} }
return Stream.empty(); return Stream.empty();
Expand All @@ -139,15 +140,15 @@ public static Stream<Path> list(Path path) {
public static void imageWrite(BufferedImage bufferedImage, String format, File output) { public static void imageWrite(BufferedImage bufferedImage, String format, File output) {
try { try {
ImageIO.write(bufferedImage, format, output); ImageIO.write(bufferedImage, format, output);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while writing buff image to {}", output, e); logger.error("Problem occured while writing buff image to {}", output, e);
} }
} }


public static byte[] readAllBytes(Path path) { public static byte[] readAllBytes(Path path) {
try { try {
return Files.readAllBytes(path); return Files.readAllBytes(path);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while reading {}", path, e); logger.error("Problem occured while reading {}", path, e);
} }
return new byte[]{}; return new byte[]{};
Expand All @@ -156,7 +157,7 @@ public static byte[] readAllBytes(Path path) {
public static void move(Path source, Path target, StandardCopyOption... option) { public static void move(Path source, Path target, StandardCopyOption... option) {
try { try {
Files.move(source, target, option); Files.move(source, target, option);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while moving {} to {}", source, target, e); logger.error("Problem occured while moving {} to {}", source, target, e);
} }
} }
Expand Down Expand Up @@ -191,15 +192,15 @@ public static void transform(Transformer transformer, StreamSource xmlSource, St
public static void matchWrite(Match root, File file) { public static void matchWrite(Match root, File file) {
try { try {
root.write(file); root.write(file);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while writing XML Match to {}", file, e); logger.error("Problem occured while writing XML Match to {}", file, e);
} }
} }


public static void copyDirectoryToDirectory(File source, File target) { public static void copyDirectoryToDirectory(File source, File target) {
try { try {
FileUtils.copyDirectoryToDirectory(source, target); FileUtils.copyDirectoryToDirectory(source, target);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while copying {} to {}", source, target, e); logger.error("Problem occured while copying {} to {}", source, target, e);
} }
} }
Expand All @@ -215,32 +216,32 @@ public static void setUserConfig(FopFactory fopFactory, String configUri) {
public static void deleteIfExists(Path path) { public static void deleteIfExists(Path path) {
try { try {
Files.deleteIfExists(path); Files.deleteIfExists(path);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while deleting {}", path, e); logger.error("Problem occured while deleting {}", path, e);
} }
} }


public static void copyDirectory(Path sourceDir, Path targetDir) { public static void copyDirectory(Path sourceDir, Path targetDir) {
try { try {
FileUtils.copyDirectory(sourceDir.toFile(), targetDir.toFile()); FileUtils.copyDirectory(sourceDir.toFile(), targetDir.toFile());
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while copying {} to {}", sourceDir, targetDir, e); logger.error("Problem occured while copying {} to {}", sourceDir, targetDir, e);
} }
} }


public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) { public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) {
try { try {
return Files.find(start, Integer.MAX_VALUE, matcher, options); return Files.find(start, Integer.MAX_VALUE, matcher, options);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while finding in path {}", start, e); logger.error("Problem occured while finding in path {}", start, e);
} }
return Stream.empty(); return Stream.empty();
} }


public static boolean isHidden(Path path) { public static boolean isHidden(Path path) {
try { try {
return Files.isHidden(path) || path.getFileName().toString().startsWith("."); return Files.exists(path) && (Files.isHidden(path) || path.getFileName().toString().startsWith("."));
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while detecting hidden path {}", path, e); logger.error("Problem occured while detecting hidden path {}", path, e);
} }
return false; return false;
Expand All @@ -249,39 +250,39 @@ public static boolean isHidden(Path path) {
public static void copyFileToDirectory(File file, File directory) { public static void copyFileToDirectory(File file, File directory) {
try { try {
FileUtils.copyFileToDirectory(file, directory); FileUtils.copyFileToDirectory(file, directory);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while copying {} to {}", file, directory, e); logger.error("Problem occured while copying {} to {}", file, directory, e);
} }
} }


public static void copyFile(File file, File dest) { public static void copyFile(File file, File dest) {
try { try {
FileUtils.copyFile(file, dest); FileUtils.copyFile(file, dest);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while copying {} to {}", file, dest, e); logger.error("Problem occured while copying {} to {}", file, dest, e);
} }
} }


public static void createDirectory(Path path) { public static void createDirectory(Path path) {
try { try {
Files.createDirectory(path); Files.createDirectory(path);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while creating {} path", path, e); logger.error("Problem occured while creating {} path", path, e);
} }
} }


public static void deleteDirectory(Path path) { public static void deleteDirectory(Path path) {
try { try {
FileUtils.deleteDirectory(path.toFile()); FileUtils.deleteDirectory(path.toFile());
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while deleting {} path", path, e); logger.error("Problem occured while deleting {} path", path, e);
} }
} }


public static List<String> readAllLines(Path path) { public static List<String> readAllLines(Path path) {
try { try {
return Files.readAllLines(path); return Files.readAllLines(path);
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while reading {} path", path, e); logger.error("Problem occured while reading {} path", path, e);
} }
return new ArrayList<>(); return new ArrayList<>();
Expand All @@ -290,7 +291,7 @@ public static List<String> readAllLines(Path path) {
public static FileReader fileReader(Path path) { public static FileReader fileReader(Path path) {
try { try {
return new FileReader(path.toFile()); return new FileReader(path.toFile());
} catch (FileNotFoundException e) { } catch (Exception e) {
logger.error("Problem occured while creating FileReader for {} path", path, e); logger.error("Problem occured while creating FileReader for {} path", path, e);
} }
return null; return null;
Expand All @@ -301,9 +302,18 @@ public static void close(Closeable... closeables) {
for (Closeable closeable : closeables) { for (Closeable closeable : closeables) {
try { try {
closeable.close(); closeable.close();
} catch (IOException e) { } catch (Exception e) {
logger.error("Problem occured while closing resource"); logger.error("Problem occured while closing resource");
} }
} }
} }

public static FileTime getLastModifiedTime(Path path) {
try {
return Files.getLastModifiedTime(path);
} catch (Exception e) {
// e.printStackTrace();
}
return null;
}
} }

0 comments on commit cf1522a

Please sign in to comment.