List of usage examples for java.io FileNotFoundException FileNotFoundException
public FileNotFoundException(String s)
FileNotFoundException
with the specified detail message. From source file:maspack.fileutil.SafeFileUtils.java
/** * Moves a file.// w w w.ja va 2 s. c om * <p> * When the destination file is on another file system, do a * "copy and delete". * * @param srcDir * the file to be moved * @param destDir * the destination file * @throws NullPointerException * if source or destination is {@code null} * @throws IOException * if source or destination is invalid * @throws IOException * if an IO error occurs moving the file * @since 1.4 */ public static void moveDirectory(File srcDir, File destDir, int options) throws IOException { if (srcDir == null) { throw new NullPointerException("Source must not be null"); } if (destDir == null) { throw new NullPointerException("Destination must not be null"); } if (!srcDir.exists()) { throw new FileNotFoundException("Source '" + srcDir + "' does not exist"); } boolean rename = srcDir.renameTo(destDir); if (!rename) { copyDirectory(srcDir, destDir, options); if (!srcDir.delete()) { throw new IOException( "Failed to delete original file '" + srcDir + "' after copy to '" + destDir + "'"); } } }
From source file:calliope.db.CouchConnection.java
/** * Fetch a resource from the server, or try to. * @param db the name of the database//from ww w. ja va2 s . c o m * @param docID the docid of the reputed resource * @return the response as a string or null if not found */ @Override public String getFromDb(String db, String docID) throws AeseException { try { //long startTime = System.currentTimeMillis(); String login = (user == null) ? "" : user + ":" + password + "@"; docID = convertDocID(docID); URL u = new URL("http://" + login + host + ":" + dbPort + "/" + db + "/" + docID); URLConnection conn = u.openConnection(); InputStream is = conn.getInputStream(); ByteHolder bh = new ByteHolder(); long timeTaken = 0, start = System.currentTimeMillis(); // HttpURLConnection seems to use non-blocking I/O while (timeTaken < bigTimeout && (is.available() > 0 || timeTaken < smallTimeout)) { if (is.available() > 0) { byte[] data = new byte[is.available()]; is.read(data); bh.append(data); // restart timeout timeTaken = 0; } else timeTaken = System.currentTimeMillis() - start; } is.close(); if (bh.isEmpty()) throw new FileNotFoundException("failed to fetch resource " + db + "/" + docID); //System.out.println("time taken to fetch from couch: " //+(System.currentTimeMillis()-startTime) ); else return new String(bh.getData(), "UTF-8"); } catch (Exception e) { throw new AeseException(e); } }
From source file:com.rapleaf.ramhdfs.RamFileSystem.java
@Override public FSDataOutputStream append(Path f, int bufferSize, Progressable progressable) throws IOException { if (!exists(f)) { throw new FileNotFoundException("File " + f + " not found."); }// w w w . j av a 2 s .c o m if (isDirectory(pathToFileObject(f))) { throw new IOException("Cannot append to a diretory (=" + f + " )."); } FileObject fo = pathToFileObject(f); return new FSDataOutputStream(new BufferedOutputStream(new RamFSOutputStream(fo), bufferSize), statistics); }
From source file:eu.mondo.driver.mongo.MongoGraphClient.java
public void load(final String modelPath, String ontologyIRI) throws IOException { // System.out.println(System.currentTimeMillis()); if (!new File(modelPath).exists()) { throw new FileNotFoundException(modelPath); }// www . j a v a 2s . co m RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE); rdfParser.setRDFHandler(new RDFHandler() { @Override public void startRDF() throws RDFHandlerException { } @Override public void handleStatement(Statement st) throws RDFHandlerException { collection.insert(st); } @Override public void handleNamespace(String arg0, String arg1) throws RDFHandlerException { } @Override public void handleComment(String arg0) throws RDFHandlerException { } @Override public void endRDF() throws RDFHandlerException { } }); // System.out.println(System.currentTimeMillis()); try { rdfParser.parse(new FileInputStream(modelPath), ontologyIRI); MongoCursor<MStatement> nulls = collection.find("{ \"subjectBI\": \"\" }").as(MStatement.class); while (nulls.hasNext()) { MStatement next = nulls.next(); String subjectBI = getBISForURI(next.getSubject().toString()); String predicateBI = getBISForURI(next.getPredicate().toString()); String objectBI = getBISForURI(next.getObject().toString()); collection .update("{ \"subject\": #, \"predicate\": #, \"object\": # }", next.getSubject().toString(), next.getPredicate().toString(), next.getObject().toString()) .with("{ $set: { \"subjectBI\": #, \"predicateBI\": #, \"objectBI\": # } }", subjectBI, predicateBI, objectBI); } } catch (IOException e) { e.printStackTrace(); } catch (RDFParseException e) { e.printStackTrace(); } catch (RDFHandlerException e) { e.printStackTrace(); } // System.out.println(System.currentTimeMillis()); }
From source file:fr.landel.utils.io.InternalFileSystemUtils.java
/** * Copy a directory recursively.//from www . jav a2 s . c om * * @param src * The source directory name * @param dest * The destination directory name, the directory is created if it * not exists * @param fileFilter * The filter to limit file to be copied * @param filenameFilter * The filename filter to limit file to be copied * @param removeSource * Remove the source after copy * @throws IOException * Exception thrown if problems occurs during coping */ protected static void copyDirectory(final File src, final File dest, final FileFilter fileFilter, final FilenameFilter filenameFilter, final boolean removeSource) throws IOException { Assertor.that(src).validates((file) -> file.exists()) .orElseThrow(() -> new FileNotFoundException("the source doesn't exist")); if (src.isDirectory()) { // creation du repertoire si necessaire if (InternalFileSystemUtils.createDirectory(dest)) { // creation de la liste des fichiers et repertoires File[] filesToCopy; if (fileFilter != null) { filesToCopy = src.listFiles(fileFilter); } else if (filenameFilter != null) { filesToCopy = src.listFiles(filenameFilter); } else { filesToCopy = src.listFiles(); } if (ArrayUtils.isNotEmpty(filesToCopy)) { copy(filesToCopy, dest, fileFilter, filenameFilter, removeSource); } if (removeSource && !src.delete()) { throw new IOException("Cannot delete the directory" + src.getAbsolutePath()); } } else { throw new IOException("cannot access or create the destination directory"); } } else if (matchFilter(src, fileFilter, filenameFilter)) { copyFile(src, dest, removeSource); } }
From source file:fr.ens.biologie.genomique.eoulsan.actions.ExecAction.java
/** * Run Eoulsan// ww w . j a va 2s .c o m * @param workflowFile workflow file * @param designFile design file * @param jobDescription job description */ private static void run(final File workflowFile, final File designFile, final String jobDescription) { checkNotNull(workflowFile, "paramFile is null"); checkNotNull(designFile, "designFile is null"); final String desc; if (jobDescription == null) { desc = "no job description"; } else { desc = jobDescription.trim(); } getLogger().info("Workflow file: " + workflowFile); getLogger().info("Design file: " + designFile); try { // Test if workflow file exists if (!workflowFile.exists()) { throw new FileNotFoundException(workflowFile.toString()); } // Test if design file exists if (!designFile.exists()) { throw new FileNotFoundException(designFile.toString()); } // Create execution context // Set job environment final String env = "Local Mode on " + new LinuxCpuInfo().getModelName() + ", " + Runtime.getRuntime().availableProcessors() + " CPU(s)/thread(s), " + new LinuxMemInfo().getMemTotal(); // Create ExecutionArgument object final ExecutorArguments arguments = new ExecutorArguments(workflowFile, designFile); arguments.setJobDescription(desc); arguments.setJobEnvironment(env); // Create the log Files Main.getInstance().createLogFiles(arguments.logPath(Globals.LOG_FILENAME), arguments.logPath(Globals.OTHER_LOG_FILENAME)); // Create executor final Executor e = new Executor(arguments); // Launch executor e.execute(); } catch (FileNotFoundException e) { Common.errorExit(e, "File not found: " + e.getMessage()); } catch (Throwable e) { Common.errorExit(e, "Error while executing " + Globals.APP_NAME_LOWER_CASE + ": " + e.getMessage()); } }
From source file:fm.last.commons.io.LastFileUtils.java
/** * Searches for a file on local filesytem, classpath etc. * //www.j ava 2 s . co m * @param fileName Name of file to find. * @param classToLoadFrom Class to use as a base for finding the file via it's classloader, if necessary. * @return The file if found on the file system. * @throws FileNotFoundException If the File could not be found. */ public static File getFile(String fileName, Class<?> classToLoadFrom) throws FileNotFoundException { File file = new File(fileName); // first try the path directly if (!file.exists()) { URL fileURL = classToLoadFrom.getResource(fileName);// next try the class's classpath if (fileURL == null) { fileURL = classToLoadFrom.getClassLoader().getResource(fileName);// next try the class' classloader's classpath if (fileURL == null) { fileURL = ClassLoader.getSystemClassLoader().getResource(fileName); // finally try the system classloader's // classpath if (fileURL == null) { throw new FileNotFoundException( "Could not find " + fileName + " on path, classpath " + "or system classpath"); } } } file = new File(fileURL.getFile()); } log.debug("Path to file located is " + file.getAbsolutePath()); return file; }
From source file:de.cosmocode.palava.services.mail.EmailFactory.java
@SuppressWarnings("unchecked") Email build(Document document, Embedder embed) throws EmailException, FileNotFoundException { /* CHECKSTYLE:ON */ final Element root = document.getRootElement(); final List<Element> messages = root.getChildren("message"); if (messages.isEmpty()) throw new IllegalArgumentException("No messages found"); final List<Element> attachments = root.getChildren("attachment"); final Map<ContentType, String> available = new HashMap<ContentType, String>(); for (Element element : messages) { final String type = element.getAttributeValue("type"); final ContentType messageType = StringUtils.equals(type, "html") ? ContentType.HTML : ContentType.PLAIN; if (available.containsKey(messageType)) { throw new IllegalArgumentException("Two messages with the same types have been defined."); }//from ww w .j a v a 2 s.c o m available.put(messageType, element.getText()); } final Email email; if (available.containsKey(ContentType.HTML) || attachments.size() > 0) { final HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setCharset(CHARSET); if (embed.hasEmbeddings()) { htmlEmail.setSubType("related"); } else if (attachments.size() > 0) { htmlEmail.setSubType("related"); } else { htmlEmail.setSubType("alternative"); } /** * Add html message */ if (available.containsKey(ContentType.HTML)) { htmlEmail.setHtmlMsg(available.get(ContentType.HTML)); } /** * Add plain text alternative */ if (available.containsKey(ContentType.PLAIN)) { htmlEmail.setTextMsg(available.get(ContentType.PLAIN)); } /** * Embedded binary data */ for (Map.Entry<String, String> entry : embed.getEmbeddings().entrySet()) { final String path = entry.getKey(); final String cid = entry.getValue(); final String name = embed.name(path); final File file; if (path.startsWith(File.separator)) { file = new File(path); } else { file = new File(embed.getResourcePath(), path); } if (file.exists()) { htmlEmail.embed(new FileDataSource(file), name, cid); } else { throw new FileNotFoundException(file.getAbsolutePath()); } } /** * Attached binary data */ for (Element attachment : attachments) { final String name = attachment.getAttributeValue("name", ""); final String description = attachment.getAttributeValue("description", ""); final String path = attachment.getAttributeValue("path"); if (path == null) throw new IllegalArgumentException("Attachment path was not set"); File file = new File(path); if (!file.exists()) file = new File(embed.getResourcePath(), path); if (file.exists()) { htmlEmail.attach(new FileDataSource(file), name, description); } else { throw new FileNotFoundException(file.getAbsolutePath()); } } email = htmlEmail; } else if (available.containsKey(ContentType.PLAIN)) { email = new SimpleEmail(); email.setCharset(CHARSET); email.setMsg(available.get(ContentType.PLAIN)); } else { throw new IllegalArgumentException("No valid message found in template."); } final String subject = root.getChildText("subject"); email.setSubject(subject); final Element from = root.getChild("from"); final String fromAddress = from == null ? null : from.getText(); final String fromName = from == null ? fromAddress : from.getAttributeValue("name", fromAddress); email.setFrom(fromAddress, fromName); final Element to = root.getChild("to"); if (to != null) { final String toAddress = to.getText(); if (StringUtils.isNotBlank(toAddress) && toAddress.contains(EMAIL_SEPARATOR)) { final String[] toAddresses = toAddress.split(EMAIL_SEPARATOR); for (String address : toAddresses) { email.addTo(address); } } else if (StringUtils.isNotBlank(toAddress)) { final String toName = to.getAttributeValue("name", toAddress); email.addTo(toAddress, toName); } } final Element cc = root.getChild("cc"); if (cc != null) { final String ccAddress = cc.getText(); if (StringUtils.isNotBlank(ccAddress) && ccAddress.contains(EMAIL_SEPARATOR)) { final String[] ccAddresses = ccAddress.split(EMAIL_SEPARATOR); for (String address : ccAddresses) { email.addCc(address); } } else if (StringUtils.isNotBlank(ccAddress)) { final String ccName = cc.getAttributeValue("name", ccAddress); email.addCc(ccAddress, ccName); } } final Element bcc = root.getChild("bcc"); if (bcc != null) { final String bccAddress = bcc.getText(); if (StringUtils.isNotBlank(bccAddress) && bccAddress.contains(EMAIL_SEPARATOR)) { final String[] bccAddresses = bccAddress.split(EMAIL_SEPARATOR); for (String address : bccAddresses) { email.addBcc(address); } } else if (StringUtils.isNotBlank(bccAddress)) { final String bccName = bcc.getAttributeValue("name", bccAddress); email.addBcc(bccAddress, bccName); } } final Element replyTo = root.getChild("replyTo"); if (replyTo != null) { final String replyToAddress = replyTo.getText(); if (StringUtils.isNotBlank(replyToAddress) && replyToAddress.contains(EMAIL_SEPARATOR)) { final String[] replyToAddresses = replyToAddress.split(EMAIL_SEPARATOR); for (String address : replyToAddresses) { email.addReplyTo(address); } } else if (StringUtils.isNotBlank(replyToAddress)) { final String replyToName = replyTo.getAttributeValue("name", replyToAddress); email.addReplyTo(replyToAddress, replyToName); } } return email; }
From source file:mitm.common.tools.PfxTool.java
private static KeyStore loadKeyStore(String keyFile, boolean shouldExist, String password) throws Exception { File file = new File(keyFile); file = file.getAbsoluteFile();//from w w w. j ava 2s. c o m KeyStore keyStore = KeyStore.getInstance("PKCS12"); if (shouldExist && !file.exists()) { throw new FileNotFoundException(keyFile + " pfx file not found."); } /* initialize key store */ char[] pw = password != null ? password.toCharArray() : null; if (file.exists()) { InputStream input = new FileInputStream(file); keyStore.load(input, pw); input.close(); } else { // creates an empty keystore keyStore.load(null, pw); } return keyStore; }
From source file:com.ariatemplates.attester.junit.Attester.java
private static String getConfigFile(Class<?> testClass) { ConfigFile configFileAnnotation = testClass.getAnnotation(ConfigFile.class); if (configFileAnnotation == null) { return null; }//from w ww .j av a 2 s . c o m File configFile = new File(configFileAnnotation.value()); if (!configFile.exists()) { throw new RuntimeException(new FileNotFoundException(configFile.getAbsolutePath())); } return configFileAnnotation.value(); }