List of usage examples for java.io FileNotFoundException FileNotFoundException
public FileNotFoundException(String s)
FileNotFoundException
with the specified detail message. From source file:Main.java
/** * By default File#delete fails for non-empty directories, it works like "rm". We need something * a little more brutual - this does the equivalent of "rm -r" * * @param path Root File Path//from w w w . j a va 2s . co m * @return true iff the file and all sub files/directories have been removed */ public static boolean deleteRecursive(File path) throws FileNotFoundException { if (!path.exists()) { throw new FileNotFoundException(path.getAbsolutePath()); } boolean ret = true; if (path.isDirectory()) { for (File f : path.listFiles()) { ret = ret && deleteRecursive(f); } } return ret && path.delete(); }
From source file:com.bysailors.comparefolders.compareFiles.java
public static boolean compareFiles(File File1, File File2) throws FileNotFoundException, IOException { if (!File1.exists() || !File2.exists()) throw new FileNotFoundException("the file specified does not exists."); else/*from ww w. ja va 2s .c o m*/ return FileUtils.contentEquals(File1, File2); }
From source file:Main.java
public static void valiFileIsExists(File file) throws FileNotFoundException { if (!file.exists()) { throw new FileNotFoundException("File '" + file.getName() + "' not found!"); }/*from ww w .ja v a 2 s .co m*/ }
From source file:Main.java
/** * Saves the data in the file in xml format. * * @param file Points to a valid xml file containing data that match the {@code classToConvert}. * Cannot be null.// w w w. j a v a2 s .c om * @throws FileNotFoundException Thrown if the file is missing. * @throws JAXBException Thrown if there is an error during converting the data * into xml and writing to the file. */ public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException { assert file != null; assert data != null; if (!file.exists()) { throw new FileNotFoundException("File not found : " + file.getAbsolutePath()); } JAXBContext context = JAXBContext.newInstance(data.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(data, file); }
From source file:Main.java
public static void unzip(File zipFile, File targetDirectory) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile))); try {// w ww . j a v a 2 s . co m ZipEntry ze; int count; byte[] buffer = new byte[8192]; while ((ze = zis.getNextEntry()) != null) { File file = new File(targetDirectory, ze.getName()); File dir = ze.isDirectory() ? file : file.getParentFile(); if (!dir.isDirectory() && !dir.mkdirs()) throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath()); if (ze.isDirectory()) continue; FileOutputStream fout = new FileOutputStream(file); try { while ((count = zis.read(buffer)) != -1) fout.write(buffer, 0, count); } finally { fout.close(); } } } finally { zis.close(); } }
From source file:Main.java
public static void unzip(InputStream fin, String targetPath, Context context) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin)); try {//from w ww. ja va 2 s . c o m ZipEntry zipEntry; int count; byte[] buffer = new byte[8192]; while ((zipEntry = zis.getNextEntry()) != null) { File file = new File(targetPath, zipEntry.getName()); File dir = zipEntry.isDirectory() ? file : file.getParentFile(); if (!dir.isDirectory() && !dir.mkdirs()) { throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath()); } if (zipEntry.isDirectory()) { continue; } FileOutputStream fout = new FileOutputStream(file); try { while ((count = zis.read(buffer)) != -1) fout.write(buffer, 0, count); } finally { fout.close(); } Log.d("TEST", "Unzipped " + file.getAbsolutePath()); } } finally { zis.close(); } }
From source file:MD5Sum.java
/** * Create a MD5 checksum for a file//from ww w. jav a 2 s . co m * * @param filename * @return md5sum * @throws Exception */ public static byte[] createChecksum(InputStream fis) throws NoSuchAlgorithmException, IOException { if (fis == null) { throw new FileNotFoundException("InputStream cannot be read"); } byte[] buffer = new byte[1024]; MessageDigest complete = MessageDigest.getInstance("MD5"); int numRead; do { numRead = fis.read(buffer); if (numRead > 0) { complete.update(buffer, 0, numRead); } } while (numRead != -1); fis.close(); return complete.digest(); }
From source file:com.hurence.logisland.config.ConfigReader.java
/** * Loads a YAML config file//from w ww. jav a 2 s . c om * * @param configFilePath the path of the config file * @return a LogislandSessionConfiguration * @throws Exception */ public static LogislandConfiguration loadConfig(String configFilePath) throws Exception { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); File configFile = new File(configFilePath); if (!configFile.exists()) { throw new FileNotFoundException("Error: File " + configFilePath + " not found!"); } return mapper.readValue(configFile, LogislandConfiguration.class); }
From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java
public static CXML unmarshallCXMLFromFile(final String relativeFilePath) throws FileNotFoundException { final InputStream fileInputStream = PunchOutUtils.class.getClassLoader() .getResourceAsStream(relativeFilePath); if (fileInputStream == null) { throw new FileNotFoundException("Could not find file [" + relativeFilePath + "]"); }// w ww. j a v a 2 s . c o m try { final JAXBContext jaxbContext = JAXBContext.newInstance(CXML.class); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return (CXML) unmarshaller.unmarshal(fileInputStream); } catch (final JAXBException e) { throw new PunchOutException(e.getErrorCode(), e.getMessage()); } }
From source file:de.sub.goobi.config.DigitalCollections.java
/** * Get possible digital collections for process. * * @param process/*from www . jav a2 s. c o m*/ * object * @return list of Strings */ @SuppressWarnings("unchecked") public static List<String> possibleDigitalCollectionsForProcess(Process process) throws JDOMException, IOException { List<String> result = new ArrayList<>(); String filename = FilenameUtils.concat(ConfigCore.getKitodoConfigDirectory(), FileNames.DIGITAL_COLLECTIONS_FILE); if (!(new File(filename).exists())) { throw new FileNotFoundException("File not found: " + filename); } /* Datei einlesen und Root ermitteln */ SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File(filename)); Element root = doc.getRootElement(); /* alle Projekte durchlaufen */ List<Element> projekte = root.getChildren(); for (Element project : projekte) { List<Element> projektnamen = project.getChildren("name"); for (Element projectName : projektnamen) { /* * wenn der Projektname aufgefhrt wird, dann alle Digitalen * Collectionen in die Liste */ if (projectName.getText().equalsIgnoreCase(process.getProject().getTitle())) { List<Element> myCols = project.getChildren("DigitalCollection"); for (Element digitalCollection : myCols) { result.add(digitalCollection.getText()); } } } } // If result is empty, get default if (result.size() == 0) { List<Element> children = root.getChildren(); for (Element child : children) { if (child.getName().equals("default")) { List<Element> myCols = child.getChildren("DigitalCollection"); for (Element digitalCollection : myCols) { result.add(digitalCollection.getText()); } } } } return result; }