Example usage for java.io FileNotFoundException toString

List of usage examples for java.io FileNotFoundException toString

Introduction

In this page you can find the example usage for java.io FileNotFoundException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:Zip.java

/**
 * Create a Writer on a given file, transparently compressing the data to a
 * Zip file whose name is the provided file path, with a ".zip" extension
 * added./*  w ww.  java  2  s . co  m*/
 *
 * @param file the file (with no zip extension)
 *
 * @return a writer on the zip entry
 */
public static Writer createWriter(File file) {
    try {
        String path = file.getCanonicalPath();
        FileOutputStream fos = new FileOutputStream(path + ".zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze = new ZipEntry(file.getName());
        zos.putNextEntry(ze);

        return new OutputStreamWriter(zos);
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (Exception ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:Zip.java

/**
 * Create a InputStream on a given file, by looking for a zip archive whose
 * path is the file path with ".zip" appended, and by reading the first
 * entry in this zip file.// ww w  .  j  a  va2 s.c o  m
 *
 * @param file the file (with no zip extension)
 *
 * @return a InputStream on the zip entry
 */
public static InputStream createInputStream(File file) {
    try {
        String path = file.getCanonicalPath();

        //ZipFile zf = new ZipFile(path + ".zip");
        ZipFile zf = new ZipFile(path);

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            return zf.getInputStream(entry);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:Zip.java

/**
 * Create a Reader on a given file, by looking for a zip archive whose path
 * is the file path with ".zip" appended, and by reading the first entry in
 * this zip file.//from w  w  w . j  a v a2s. c o  m
 *
 * @param file the file (with no zip extension)
 *
 * @return a reader on the zip entry
 */
public static Reader createReader(File file) {
    try {
        String path = file.getCanonicalPath();

        ZipFile zf = new ZipFile(path + ".zip");

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream is = zf.getInputStream(entry);

            return new InputStreamReader(is);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:io.github.retz.web.ClientHelper.java

public static void getWholeFileWithTerminator(Client c, int id, String filename, String resultDir,
        Callable<Boolean> terminator) throws IOException, TimeoutException {
    String path = resultDir + "/" + filename;
    try (FileOutputStream out = new FileOutputStream(path)) {
        getWholeFileWithTerminator(c, id, filename, false, out, terminator);

    } catch (FileNotFoundException e) {
        LOG.error(e.toString());
    } catch (JobNotFoundException e) {
        LOG.error(e.toString());/*from  w w w . j ava2 s .c  om*/
    }
}

From source file:org.openinfinity.cloud.util.filesystem.FileUtil.java

public static InputStream loadInputStreamByFileName(String filePath) {
    InputStream inputStream = null;
    try {/* w w  w  .ja v  a  2  s.c  o m*/
        File file = new File(filePath);
        checkAvailability(EXCEPTION_CAUSE_FILE_DOES_NOT_EXIST, file);
        inputStream = new FileInputStream(file);
    } catch (FileNotFoundException fileNotFoundException) {
        ExceptionUtil.throwSystemException(EXCEPTION_CAUSE_IO_ERROR + ": " + fileNotFoundException.toString(),
                fileNotFoundException);
    }
    return inputStream;
}

From source file:org.disit.servicemap.ServiceMap.java

static private void loadStopWords() {
    if (stopWords != null)
        return; //already loaded;

    stopWords = new ArrayList<String>();
    String token1 = "";
    try {/*ww  w .j a  v  a2  s  . c  o m*/
        // create Scanner
        Scanner scanner = new Scanner(new File(System.getProperty("user.home") + "/servicemap/stop_words.txt"));
        while (scanner.hasNext()) {
            // find next line
            token1 = scanner.next();
            //System.out.println("-"+token1+"-");
            stopWords.add(token1);
        }
        scanner.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Exception: File not found " + ex.toString());
    }
}

From source file:org.sherlok.FileBased.java

/**
 * Read a JSON-serialized object from file and parse it back to an object.
 * Performs extensive error-catching to provide useful information in case
 * of error.//  w  w  w  .  ja  v  a  2  s .c  o  m
 * 
 * @param f
 *            the file to read
 * @param clazz
 *            the class to cast this object into
 * @return the parsed object
 * @throws SherlokException
 *             if the object cannot be found or parsed
 */
public static <T> T read(File f, Class<T> clazz) throws SherlokException {
    try {
        return MAPPER.readValue(new FileInputStream(f), clazz);

    } catch (FileNotFoundException io) {
        throw new SherlokException()
                .setMessage(clazz.getSimpleName().replaceAll("Def$", "") + " does not exist.")
                .setObject(f.getName()).setDetails(io.toString());

    } catch (UnrecognizedPropertyException upe) {
        throw new SherlokException().setMessage("Unrecognized field \"" + upe.getPropertyName() + "\"")
                .setObject(clazz.getSimpleName().replaceAll("Def$", "") + " " + f.getName())
                .setDetails(upe.getMessageSuffix());

    } catch (JsonMappingException jme) {

        StringBuilder sb = new StringBuilder();
        sb.append("cannot read ");
        jme.getPathReference(sb);
        sb.append(" in '" + f.getName() + "': ");
        sb.append(" " + jme.getOriginalMessage());

        throw new SherlokException()
                .setMessage(sb.toString().replaceAll("org\\.sherlok\\.mappings\\.\\w+Def\\[", "["));

    } catch (JsonParseException jpe) {

        throw new SherlokException().setMessage("Could not parse JSON object of type " + clazz.getSimpleName())
                .setObject(f.getName().replaceAll("Def$", "")).setDetails(jpe.getMessage());

    } catch (Exception e) {
        throw new SherlokException().setMessage(e.getMessage()).setObject(f.getName().replaceAll("Def$", ""))
                .setDetails(e.getStackTrace());
    }
}

From source file:eu.sisob.uma.extractors.adhoc.websearchers_cv.WebSearchersCVExtractor.java

/**
 *
 * @param input_file//ww  w.j  a v  a  2  s.c  om
 * @param results_dir
 * @param zip_output_file
 * @param output_file_2
 * @param error_sw
 */
public static void download_files(File input_file, File results_dir, File zip_output_file, File output_file_2,
        StringWriter error_sw) {
    CSVReader reader = null;
    try {
        reader = new CSVReader(new FileReader(input_file), CSV_SEPARATOR);
    } catch (FileNotFoundException ex) {
        Logger.getRootLogger().error("Error reading " + input_file.getName() + " - " + ex.toString());
        return;
    }

    int idStaffIdentifier = -1;
    int idName = -1;
    int idFirstName = -1;
    int idLastName = -1;
    int idInitials = -1;
    int idUnitOfAssessment_Description = -1;
    int idInstitutionName = -1;
    int idWebAddress = -1;
    int idResearchGroupDescription = -1;
    int idResearcherWebAddress = -1;
    int idResearcherWebAddressType = -1;
    int idResearcherWebAddressExt = -1;
    int idScoreUrl = -1;

    String[] nextLine;
    try {
        if ((nextLine = reader.readNext()) != null) {
            //Locate indexes            
            //Locate indexes                        
            for (int i = 0; i < nextLine.length; i++) {
                String column_name = nextLine[i];
                if (column_name.equals(FileFormatConversor.CSV_COL_ID))
                    idStaffIdentifier = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_NAME))
                    idName = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_FIRSTNAME))
                    idFirstName = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_LASTNAME))
                    idLastName = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_INITIALS))
                    idInitials = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_SUBJECT))
                    idUnitOfAssessment_Description = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_NAME))
                    idInstitutionName = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_URL))
                    idWebAddress = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_URL))
                    idResearcherWebAddress = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_TYPE))
                    idResearcherWebAddressType = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_EXT))
                    idResearcherWebAddressExt = i;
                else if (column_name.equals(FileFormatConversor.CSV_COL_SCORE_URL))
                    idScoreUrl = i;
            }

        }
    } catch (Exception ex) {
        String error_msg = "Error reading headers of " + input_file.getName();
        Logger.getRootLogger().error(error_msg + " - " + ex.toString());
        if (error_sw != null)
            error_sw.append(error_msg + "\r\n");

        return;
    }

    if (idResearcherWebAddress != -1 && idResearcherWebAddressType != -1 && idResearcherWebAddressExt != -1) {
        Logger.getRootLogger().info("Going to downloads results files");

        try {
            for (int i = 0; i < nextLine.length; i++)
                nextLine[i] = "\"" + nextLine[i] + "\"";

            FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8",
                    false);
        } catch (IOException ex) {
            Logger.getLogger("root").error(ex.toString());
        }

        if (!results_dir.exists())
            results_dir.mkdirs();
        try {
            while ((nextLine = reader.readNext()) != null) {
                String url = nextLine[idResearcherWebAddress];
                String ext = nextLine[idResearcherWebAddressExt];
                String id = nextLine[idStaffIdentifier];

                try {
                    Logger.getRootLogger().info("Downloading " + url);
                    String filename = id + "." + ext;
                    FileUtils.copyURLToFile(new URL(url), new File(results_dir, filename));

                    nextLine[idResearcherWebAddress] = filename;
                    try {
                        for (int i = 0; i < nextLine.length; i++) {
                            nextLine[i] = "\"" + nextLine[i] + "\"";
                        }
                        FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n",
                                "UTF-8", true);
                    } catch (IOException ex) {
                        Logger.getLogger("root").error(ex.toString());
                    }

                } catch (IOException ex) {
                    Logger.getRootLogger().error("Error downloading " + url);
                }
            }
        } catch (IOException ex) {
            Logger.getRootLogger().error("Error reading " + input_file.getName() + " " + ex.getMessage());
        }

        ZipFile zf;
        try {
            zip_output_file.delete();
            zf = new ZipFile(zip_output_file);
            zf.createZipFileFromFolder(results_dir, new ZipParameters(), false, 0);
        } catch (ZipException ex) {
            Logger.getRootLogger().error("Error zipping results from " + input_file.getName());
        }

    } else {
        Logger.getRootLogger().error("Headers incorrect " + input_file.getName());
    }
}

From source file:com.android.providers.downloads.OmaDownload.java

/**
* This method parses an xml file into a provided component.
@param  ddUrl       the URL of the download descriptor file
@param  file        the file containing the XML to be parsed
@param  component   the component to which the parsed xml data should be added
@return             the status code (success or other error code)
*/// ww w .  ja v a 2  s  .  com
protected static int parseXml(URL ddUrl, File file, OmaDescription component) {

    BufferedReader sReader = null;

    //Initialize the status code in the component
    component.setStatusCode(OmaStatusHandler.SUCCESS);

    if (file == null || ddUrl == null) {
        component.setStatusCode(OmaStatusHandler.INVALID_DESCRIPTOR);
    } else {
        try {
            sReader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
        }

        try {
            Xml.parse(sReader, OMADL_INSTANCE.new DDHandler(ddUrl, component));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            component.setStatusCode(OmaStatusHandler.INVALID_DESCRIPTOR);

            //parse install notify url
            String strLine;
            try {
                sReader = new BufferedReader(new FileReader(file));

                while ((strLine = sReader.readLine()) != null) {
                    strLine = strLine.trim();

                    StringBuffer strBuffer = new StringBuffer(strLine);
                    String startTag = "<installNotifyURI>";
                    String endTag = "</installNotifyURI>";
                    int startTagPos = strBuffer.lastIndexOf(startTag);
                    int endTagPos = strBuffer.lastIndexOf(endTag);

                    if (startTagPos != -1 && endTagPos != -1) {
                        strLine = strLine.substring(startTagPos + startTag.length(), endTagPos);
                        Log.d("@M_" + Constants.LOG_OMA_DL, "install notify URI: " + strLine);
                        URL url = new URL(strLine);
                        component.setInstallNotifyUrl(url);
                        break;
                    }
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                Log.e("@M_" + Constants.LOG_OMA_DL, e1.toString());
            }
        }
    }

    return component.getStatusCode();
}

From source file:org.protocoderrunner.utils.FileIO.java

public static String writeStringToFile(String url, String name, String code) {
    MLog.d(TAG, "Writing string to file name: " + name + " code: " + code);
    String filename = name.replaceAll("[^a-zA-Z0-9-_\\. ]", "_");
    String baseDir = url + File.separator + filename;
    File dir = new File(baseDir);
    dir.mkdirs();//  ww w .  ja va  2  s . c  om
    File f = new File(dir.getAbsoluteFile() + File.separator + "main.js");

    try {
        if (!f.exists()) {
            f.createNewFile();
        } else {
            // We should probably do something here to handle multiple file
            // cases
        }
        FileOutputStream fo = new FileOutputStream(f);
        byte[] data = code.getBytes();
        fo.write(data);
        fo.flush();
        fo.close();
    } catch (FileNotFoundException ex) {
        MLog.e(TAG, ex.toString());
    } catch (IOException e) {
        e.printStackTrace();
        MLog.e(TAG, e.toString());
    }
    return f.getAbsolutePath();
}