List of usage examples for java.io File canRead
public boolean canRead()
From source file:Main.java
public static void copyFile(File fromFile, File toFile, Boolean rewrite) { if (!fromFile.exists()) { return;/* ww w . j a v a2 s .c o m*/ } if (!fromFile.isFile()) { return; } if (!fromFile.canRead()) { return; } if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } if (toFile.exists() && rewrite) { toFile.delete(); } try { java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile); java.io.FileOutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[1024]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosfrom.close(); fosto.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.apache.jmeter.protocol.http.sampler.HttpClientDefaultParameters.java
private static void load(String file, GenericHttpParams params) { log.info("Trying httpclient parameters from " + file); File f = new File(file); if (!(f.exists() && f.canRead())) { f = new File(NewDriver.getJMeterDir() + File.separator + "bin" + File.separator + file); // $NON-NLS-1$ log.info(file + " httpclient parameters does not exist, trying " + f.getAbsolutePath()); if (!(f.exists() && f.canRead())) { log.error("Cannot read parameters file for HttpClient: " + file); return; }/*from ww w. j a v a2 s . c o m*/ } log.info("Reading httpclient parameters from " + f.getAbsolutePath()); InputStream is = null; Properties props = new Properties(); try { is = new FileInputStream(f); props.load(is); for (Map.Entry<Object, Object> me : props.entrySet()) { String key = (String) me.getKey(); String value = (String) me.getValue(); int typeSep = key.indexOf('$'); // $NON-NLS-1$ try { if (typeSep > 0) { String type = key.substring(typeSep + 1);// get past separator String name = key.substring(0, typeSep); log.info("Defining " + name + " as " + value + " (" + type + ")"); if (type.equals("Integer")) { params.setParameter(name, Integer.valueOf(value)); } else if (type.equals("Long")) { params.setParameter(name, Long.valueOf(value)); } else if (type.equals("Boolean")) { params.setParameter(name, Boolean.valueOf(value)); } else if (type.equals("HttpVersion")) { // Commons HttpClient only params.setVersion(name, value); } else { log.warn("Unexpected type: " + type + " for name " + name); } } else { log.info("Defining " + key + " as " + value); params.setParameter(key, value); } } catch (Exception e) { log.error("Error in property: " + key + "=" + value + " " + e.toString()); } } } catch (IOException e) { log.error("Problem loading properties " + e.toString()); } finally { JOrphanUtils.closeQuietly(is); } }
From source file:com.elasticbox.jenkins.k8s.cfg.PluginInitializer.java
private static String checkLocalKubernetesToken() { File file = new File(KUBE_TOKEN_PATH); if (file.exists() && file.canRead()) { try {// www . ja v a 2s . com String token = IOUtils.toString(file.toURI()); TokenCredentialsImpl kubeServiceToken = new TokenCredentialsImpl(CredentialsScope.SYSTEM, KUBE_SERVICE_TOKEN_ID, "Local Kubernetes service token", Secret.fromString(token)); SystemCredentialsProvider.getInstance().getCredentials().add(kubeServiceToken); SystemCredentialsProvider.getInstance().save(); return KUBE_SERVICE_TOKEN_ID; } catch (IOException excep) { LOGGER.warning("Unable to read Kubernetes service token file: " + excep); } } return StringUtils.EMPTY; }
From source file:com.dahl.brendan.wordsearch.view.IOService.java
private static void readFile(Context context, File file, boolean overwrite) { if (file.canRead()) { if (overwrite) { context.getContentResolver().delete(Word.CONTENT_URI, "1", null); }/*w w w. j av a2 s . c o m*/ try { byte[] buffer = new byte[(int) file.length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(file)); f.read(buffer); JSONArray array = new JSONArray(new String(buffer)); List<ContentValues> values = new LinkedList<ContentValues>(); for (int i = 0; i < array.length(); i++) { Log.v(LOGTAG, array.getString(i)); ContentValues value = new ContentValues(); value.put(Word.WORD, array.getString(i)); values.add(value); } context.getContentResolver().bulkInsert(Word.CONTENT_URI, values.toArray(new ContentValues[0])); } catch (IOException e) { Log.e(LOGTAG, "IO error", e); } catch (JSONException e) { Log.e(LOGTAG, "bad input", e); } } }
From source file:edu.kit.dama.staging.util.StagingUtils.java
/** * Get the temporary directory all transfers. This directory is located in the * user home directory under ~/.lsdf/. Within the temporary directory the * transfer can store status information or checkpoint data to be able to * resume failed transfers./*from w w w .j a va 2 s .c o m*/ * * @return The transfers temporary directory * * @throws IOException If there was not set any TID for this transfer or if * there are problems getting the user's home directory */ public static String getTempDir() throws IOException { File userHome = SystemUtils.getUserHome(); if (userHome == null || !userHome.isDirectory() || !userHome.canRead() || !userHome.canWrite()) { throw new IOException("Invalid user home directory '" + userHome + "'"); } return FilenameUtils.concat(userHome.getCanonicalPath(), ".lsdf"); }
From source file:test.org.tradex.camel.TestCaseAppContextBuilder.java
/** * Creates a new application context using the file found at <code>ROOT_RESOURCE_DIR + clazz.getPackageName + fileName</code> * @param fileName The filename in the calculated directory * @param clazz The clazz we're launching the app context for * @return the app context/*from ww w.j av a 2s . c o m*/ */ public static GenericXmlApplicationContext buildFor(String fileName, Class<?> clazz) { if (clazz == null) throw new IllegalArgumentException("Passed class was null", new Throwable()); File springXml = new File( ROOT_RESOURCE_DIR + clazz.getPackage().getName().replace('.', '/') + "/" + fileName); if (!springXml.canRead()) { throw new RuntimeException("Failed to read Spring XML file at [" + springXml + "]", new Throwable()); } return service(new GenericXmlApplicationContext( new FileSystemResource[] { new FileSystemResource(springXml.getAbsoluteFile()) })); }
From source file:de.micromata.genome.util.runtime.Log4JInitializer.java
/** * Try to find a log4j-dev.properies file. * * @param log4jfile the file for the log4j settings * @return the file/*from www. j a v a2 s . co m*/ */ private static File findDevFile(File log4jfile) { File dir = log4jfile.getParentFile(); String name = log4jfile.getName(); int lidx = name.lastIndexOf('.'); if (lidx == -1) { return null; } String nname = name.substring(0, lidx) + "-dev" + name.substring(lidx); File nf = new File(dir, nname); if (nf.exists() == true && nf.canRead() == true) { return nf; } return null; }
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 {/*from w w w. j ava 2s. c o 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:eu.qualimaster.adaptation.platform.ToolBase.java
/** * Returns whether <code>file</code> is readable. * //from w ww. j a v a 2 s . c om * @param file the file to be checked * @return <code>true</code> if <code>file</code> is readable, <code>false</code> else */ protected static boolean isReadable(File file) { return file.exists() && file.isFile() && file.canRead(); }
From source file:ca.uviccscu.lp.persistence.GamelistStorage.java
public static synchronized int verifyConfig(Object[] vals, boolean edit) { boolean a = ((String) vals[0] != null) && !((String) vals[0]).equals(""); if (!edit) {/* w w w. j a v a 2 s . com*/ a = a && (getGame((String) vals[0]) == null); } File fl = new File((String) vals[1]); boolean b = fl.canRead() && fl.isDirectory() && fl.isAbsolute(); fl = new File((String) vals[2]); boolean c = fl.canRead() && fl.isFile() && fl.isAbsolute(); boolean d = true; //boolean d = ((String) vals[3]).([]()/\+{}?*+.^$); boolean e = true; String ss1 = ((String) vals[4]); /* String[] sa1 = ((String) vals[4]).split("\\r?\\n"); for (String s : sa1) { * */ String[] ss1split = ss1.split("\\r?\\n"); for (String sss : ss1split) { if (!sss.equals("")) { if (!ss1.endsWith(".bat")) { e = false; l.trace("File location \"" + ss1 + "\" doesnt end with .bat"); } File f = new File(sss); if (!f.exists()) { e = false; l.trace("File location \"" + ss1 + "\" doesnt exist"); } } } if (a && b && c && d && e) { return -1; } else { if (a != true) { return 0; } else if (b != true) { return 1; } else if (c != true) { return 2; } else if (d != true) { return 3; } else if (e != true) { return 4; } else { return -1; } } }