Example usage for java.io File isFile

List of usage examples for java.io File isFile

Introduction

In this page you can find the example usage for java.io File isFile.

Prototype

public boolean isFile() 

Source Link

Document

Tests whether the file denoted by this abstract pathname is a normal file.

Usage

From source file:com.tinspx.util.io.callbacks.FileTests.java

public static boolean deleteIfExists(File file) {
    if (file.exists()) {
        checkArgument(file.isFile(), "%s is not a file", file);
        if (!file.delete()) {
            throw new RuntimeException("could not delete: " + file);
        }/*from   w ww.ja v  a  2 s.c o m*/
        return true;
    }
    return false;
}

From source file:com.tinspx.util.io.callbacks.FileTests.java

public static void delete(File file) {
    if (file.exists()) {
        checkArgument(file.isFile(), "%s is not a file", file);
        if (!file.delete()) {
            throw new RuntimeException("could not delete: " + file);
        }/*from w w  w .  j a  v  a2  s. c  o m*/
    } else {
        throw new RuntimeException(file + " DNE");
    }
}

From source file:net.redstonelamp.gui.RedstoneLampGUI.java

private static void addHistory(File file) {
    File config = new File("redstonelamp-gui-config.json");
    if (!config.isFile()) {
        try {/*  w w w . j av a  2 s. com*/
            InputStream is = RedstoneLampGUI.class.getResourceAsStream("redfstonelamp-gui-config.json");
            OutputStream os = new FileOutputStream(config);
            IOUtils.copy(is, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        InputStream is = new FileInputStream(config);
        byte[] buffer = new byte[is.available()];
        is.read(buffer);
        is.close();
        try {
            JSONObject json = new JSONObject(new String(buffer));
            JSONArray array = json.getJSONArray("history");
            JSONObject object = new JSONObject();
            object.put("path", file.getCanonicalPath());
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:eu.planets_project.ifr.core.techreg.formats.DroidConfig.java

/**
 * @return The location of the DROID signature file taken from the
 *          Droid configuration properties file
 *///from w  ww.  ja va 2 s . c  o  m
public static String getSigFileLocation() {
    // String to hold the location
    String sigFileLocation = null;
    // Get the configuration object from the ServiceConfig util
    try {
        Configuration conf = ServiceConfig.getConfiguration(COMMON_CONF_FILE_NAME);
        // Create the file name from the properties
        sigFileLocation = conf.getString(SIG_FILE_LOC_KEY) + File.separator + conf.getString(SIG_FILE_NAME_KEY);
    } catch (ConfigurationException e) {
        log.severe("Could not find configuration file! " + e);
    }
    log.info("DROID Signature File location:" + sigFileLocation);
    // Check if the sigFileLocation is sane, and override with internal resource if not:
    File sfl = null;
    if (sigFileLocation != null)
        sfl = new File(sigFileLocation);
    if (sfl == null || !sfl.exists() || !sfl.isFile()) {
        try {
            File tmp = File.createTempFile("DroidSigFile", "xml");
            tmp.deleteOnExit();
            IOUtils.copy(DroidConfig.class.getResourceAsStream("/droid/DROID_SignatureFile.xml"),
                    new FileOutputStream(tmp));
            sigFileLocation = tmp.getAbsolutePath();
            log.info("Wrote cached Droid sig file to " + sigFileLocation);
        } catch (IOException e) {
            e.printStackTrace();
            log.severe("Could not generate external Droid Sig File.");
        }
    }
    return sigFileLocation;
}

From source file:net.sf.jvifm.ui.Util.java

public static void editFile(String pwd, String path) {
    Preference preference = Preference.getInstance();
    String EDITOR = preference.getEditorApp();
    File file = new File(path);
    if (file.isFile() && file.canRead()) {
        String ext = FilenameUtils.getExtension(path);
        if (ext.equals("zip") || ext.equals("jar") || ext.equals("war")) { //$NON-NLS-1$

            Util.openFileWithDefaultApp(path);
            //Main.fileManager.zipTabNew(path);
        } else {//  w  w w.  jav a  2  s . co  m
            try {
                String param1 = "-p";
                String param2 = "--remote-tab-silent";
                String param3 = "--servername";
                String param4 = "JVIFM";

                String cmd[] = { EDITOR, param3, param4, param1, param2, path };
                // String cmd[]={EDITOR , path};
                Runtime.getRuntime().exec(cmd, null, new File(pwd));
            } catch (Exception e) {
                Util.openFileWithDefaultApp(path);
            }
        }
    }

}

From source file:de.tudarmstadt.ukp.dkpro.lexsemresource.core.ResourceFactory.java

public static synchronized ResourceFactory getInstance() throws ResourceLoaderException {
    if (loader == null) {
        List<String> locs = new ArrayList<String>();
        URL resourceXmlUrl = null;

        // Check in workspace
        try {//from  www  .j  av a  2  s  .  c  om
            File f = new File(getWorkspace(), CONFIG_FILE);
            if (f.isFile()) {
                try {
                    resourceXmlUrl = f.toURI().toURL();
                } catch (MalformedURLException e) {
                    throw new ResourceLoaderException(e);
                }
            }
            locs.add(f.getAbsolutePath());
        } catch (IOException e) {
            locs.add("DKPro workspace not available");
        }

        // Check in classpath
        if (resourceXmlUrl == null) {
            resourceXmlUrl = ResourceFactory.class.getResource(CONFIG_FILE);
            locs.add("Classpath: " + CONFIG_FILE);
        }

        // Check in default file system location
        if (resourceXmlUrl == null && new File(CONFIG_FILE).isFile()) {
            try {
                resourceXmlUrl = new File(CONFIG_FILE).toURI().toURL();
            } catch (MalformedURLException e) {
                throw new ResourceLoaderException(e);
            }
            locs.add(new File(CONFIG_FILE).getAbsolutePath());
        }

        // Bail out if still not found
        if (resourceXmlUrl == null) {
            throw new ResourceLoaderException(
                    "Unable to locate configuration file [" + CONFIG_FILE + "] in " + locs.toString());
        }

        loader = new ResourceFactory(resourceXmlUrl.toString());
    }
    return loader;
}

From source file:edu.usc.polar.NLTKRest.java

public static void dir(String path, String[] args) {
    try {/*from   w  w w .j  a v a2 s.c  o  m*/

        File root = new File(path);
        if (root.isFile()) {

            if (counter >= 1000 || file == null) {
                counter = 0;
                jsonCount++;
                file = new File("C:\\Users\\Snehal\\Documents\\tikaSimilarityTestSet\\NLTKRest\\NER_"
                        + jsonCount + ".json");
                if (jsonFile != null) {
                    jsonFile.write("{\"NER_NLTKRest\":");
                    jsonFile.write(jsonArray.toJSONString());
                    jsonFile.write("}");
                    //   System.out.println(jsonArray.toJSONString());
                    jsonFile.close();
                }
                jsonFile = new FileWriter(file);
                jsonArray = new JSONArray();
            }

            if (!root.getName().equals((".DS_Store"))) {
                ApacheNLTKRest(root.getAbsolutePath(), args);
                counter++;
            }
        } else {
            File[] list = root.listFiles();
            if (list == null) {
                return;
            }
            for (File f : list) {
                if (f.isDirectory()) {
                    dir(f.getAbsolutePath(), args);
                    System.out.println("Dir:" + f.getAbsoluteFile());
                } else {
                    if (counter >= 1000 || file == null) {
                        counter = 0;
                        jsonCount++;
                        file = new File("C:\\Users\\Snehal\\Documents\\tikaSimilarityTestSet\\NLTKRest\\NER_"
                                + jsonCount + ".json");
                        //  System.out.print("check"+jsonArray.toJSONString());
                        if (jsonFile != null) {
                            jsonFile.write("{\"NER_NLTKRest\":");
                            jsonFile.write(jsonArray.toJSONString());
                            jsonFile.write("}");
                            System.out.println(jsonArray.toJSONString());
                            jsonFile.close();
                        }
                        jsonFile = new FileWriter(file);
                        jsonArray = new JSONArray();
                    }

                    if (!f.getName().equals((".DS_Store"))) {
                        ApacheNLTKRest(f.getAbsolutePath(), args);
                        counter++;
                        // add json   
                    }
                }
            }
        }
    } catch (Exception e) {
        System.err.print(e.toString());

    }
}

From source file:fileserver.Utils.java

public static void init() {
    File folder = new File(dataDirectory);
    File[] listOfFiles = folder.listFiles();
    for (File f : listOfFiles) {
        if (f.isFile()) {
            String id = calcID(f);
            map.put(id, f.getName());//  w w w.j  a  v  a  2s  .  c o m
        }
    }
}

From source file:Main.java

/**
 * Reads the given file, translating {@link IOException} to a
 * {@link RuntimeException} of some sort.
 *
 * @param file {@code non-null;} the file to read
 * @return {@code non-null;} contents of the file
 *//*w  w w.  j a  v a  2 s.  c om*/
public static byte[] readFile(File file) {
    if (!file.exists()) {
        throw new RuntimeException(file + ": file not found");
    }

    if (!file.isFile()) {
        throw new RuntimeException(file + ": not a file");
    }

    if (!file.canRead()) {
        throw new RuntimeException(file + ": file not readable");
    }

    long longLength = file.length();
    int length = (int) longLength;
    if (length != longLength) {
        throw new RuntimeException(file + ": file too long");
    }

    byte[] result = new byte[length];

    try {
        FileInputStream in = new FileInputStream(file);
        int at = 0;
        while (length > 0) {
            int amt = in.read(result, at, length);
            if (amt == -1) {
                throw new RuntimeException(file + ": unexpected EOF");
            }
            at += amt;
            length -= amt;
        }
        in.close();
    } catch (IOException ex) {
        throw new RuntimeException(file + ": trouble reading", ex);
    }

    return result;
}

From source file:com.thoughtworks.gauge.util.GaugeUtil.java

private static boolean isValidGaugeExec(File file) {
    return file.exists() && file.isFile() && file.canExecute();
}