public static void copyFromResource( final String prefix, final String fileName, final File destinationDirectory) { final String pathToResource = prefix + fileName; InputStream in = null; OutputStream out = null; try { final URL resource; if (pathToResource.startsWith("file:")) { resource = new URL(pathToResource); } else { resource = ResourceController.getResourceController().getResource(pathToResource); } if (resource == null) { LogUtils.severe("Cannot find resource: " + pathToResource); return; } in = new BufferedInputStream(resource.openStream()); out = new FileOutputStream(new File(destinationDirectory, fileName)); FileUtils.copyStream(in, out); } catch (final Exception e) { LogUtils.severe( "File not found or could not be copied. " + "Was searching for " + pathToResource + " and should go to " + destinationDirectory.getAbsolutePath()); } finally { FileUtils.silentlyClose(in, out); } }
/** to be used in a finally block. This method is null-safe. */ public static void silentlyClose(Closeable... streams) { for (Closeable stream : streams) { if (stream != null) { try { stream.close(); } catch (IOException e) { LogUtils.severe(e); } } } }
/** * In case of trouble, the method returns null. * * @param pInputFile the file to read. * @return the complete content of the file. or null if an exception has occured. */ public static String readFile(final File pInputFile) { final StringBuilder lines = new StringBuilder(); BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new FileReader(pInputFile)); final String endLine = System.getProperty("line.separator"); String line; while ((line = bufferedReader.readLine()) != null) { lines.append(line).append(endLine); } bufferedReader.close(); } catch (final Exception e) { LogUtils.severe(e); if (bufferedReader != null) { try { bufferedReader.close(); } catch (final Exception ex) { LogUtils.severe(ex); } } return null; } return lines.toString(); }
public static void setHidden(final File file, final boolean hidden, final boolean synchronously) { final String osNameStart = System.getProperty("os.name").substring(0, 3); if (osNameStart.equals("Win")) { try { Controller.exec("attrib " + (hidden ? "+" : "-") + "H \"" + file.getAbsolutePath() + "\""); if (!synchronously) { return; } int timeOut = 10; while (file.isHidden() != hidden && timeOut > 0) { Thread.sleep(10 /* miliseconds */); timeOut--; } } catch (final Exception e) { LogUtils.severe(e); } } }
public static void copyFromURL(final URL resource, final File destinationDirectory) { final String path = resource.getPath(); final int index = path.lastIndexOf('/'); final String fileName = index > -1 ? path.substring(index + 1) : path; InputStream in = null; OutputStream out = null; try { in = resource.openStream(); out = new FileOutputStream(new File(destinationDirectory, fileName)); FileUtils.copyStream(in, out); } catch (final Exception e) { LogUtils.severe( "File not found or could not be copied. " + "Was searching for " + path + " and should go to " + destinationDirectory.getAbsolutePath()); } finally { FileUtils.silentlyClose(in, out); } }