List of usage examples for java.io File canRead
public boolean canRead()
From source file:test.org.tradex.camel.TestCaseAppContextBuilder.java
/** * Builds an application context reading the file from the passed path * @param path The path of the app context xml * @return the built app context//from w w w .j a v a2s.co m */ public static GenericXmlApplicationContext buildFor(String path) { if (path == null) throw new IllegalArgumentException("Passed path was null", new Throwable()); File springXml = new File(path); if (!springXml.canRead()) { throw new RuntimeException("Failed to read Spring XML file at [" + springXml + "]", new Throwable()); } try { return service(new GenericXmlApplicationContext( new FileSystemResource[] { new FileSystemResource(springXml.getAbsoluteFile()) })); } catch (Throwable t) { LOG.fatal("Failed to boot app context", t); throw new RuntimeException("Failed to boot app context", t); } }
From source file:Main.java
/**resource methods*/ public static Bitmap getBitmap(Context context, String url) { Bitmap bitmap = null;/* w ww . ja v a 2s . c o m*/ InputStream is = null; try { url = url.trim(); if (url.indexOf("res://") == 0) { int sepPos = url.lastIndexOf("/"); int resId = context.getResources().getIdentifier(url.substring(sepPos + 1), url.substring(6, sepPos), context.getPackageName()); is = context.getResources().openRawResource(resId); } else if (url.indexOf("file://") == 0) { File file = new File(url.substring(7)); if (file.canRead()) is = new FileInputStream(file); } BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = true; bitmap = BitmapFactory.decodeStream(is, new Rect(), options); return bitmap; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } } } }
From source file:de.suse.swamp.util.FileUtils.java
/** * Return the text content of a file//from ww w .j ava 2 s . co m */ public static String getText(File file) throws Exception { if (file != null && file.exists() && file.canRead()) { byte[] b = new byte[(int) file.length()]; log.debug("Getting text from file: " + file); FileInputStream filestream = null; filestream = new FileInputStream(file); filestream.read(b); filestream.close(); return new String(b); } else { throw new Exception("Could not read from file: " + file.getAbsolutePath()); } }
From source file:ConnectionUtil.java
/** Sets the full path of the config file to read. * @param configFileNam The FileName of the configuration file to use. *//* ww w . ja va 2 s. c om*/ public static void setConfigFileName(String configFileNam) { configFileName = configFileNam; File file = new File(configFileName); if (!file.canRead()) { throw new IllegalArgumentException("Unreadable: " + configFileName); } try { ConnectionUtil.configFileName = file.getCanonicalPath(); } catch (IOException ex) { System.err.println("Warning: IO error checking path: " + configFileName); ConnectionUtil.configFileName = configFileName; } }
From source file:Configuration.ConfigHelper.java
public static File getConfigFile(String fileName) throws NoConfigFileFoundException { for (int i = 0; i < CONFIG_LOCATIONS.length; i++) { String location = CONFIG_LOCATIONS[i]; if (location.startsWith("%HOME%")) { if (isUnix()) { String home = getUserHome(); if (home == null || home.length() <= 0) { continue; }/*from w w w .j a va2 s . c o m*/ location = location.replace("%HOME%", home); } else { continue; } } File dir = new File(location); if (!dir.isDirectory() || !dir.exists()) { continue; } File configFile = new File(dir, fileName); if (!configFile.exists() || !configFile.canRead()) { continue; } return configFile; } throw new NoConfigFileFoundException(); }
From source file:Main.java
public static File[] getMountedVolumesAsFile() { final String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // we can read the External Storage... //Retrieve the primary External Storage: final File primaryExternalStorage = Environment.getExternalStorageDirectory(); //Retrieve the External Storages root directory: String externalStorageRootDir = null; if ((externalStorageRootDir = primaryExternalStorage.getParent()) == null) { // no parent... return (new File[] { primaryExternalStorage }); } else {/*from ww w. j a va 2 s . com*/ final File externalStorageRoot = new File(externalStorageRootDir); final File[] files = externalStorageRoot.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { // TODO Auto-generated method stub File file = new File(dir, filename); if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()) { return true; } return false; } }); //.listFiles(); List<File> data = new ArrayList<File>(); for (final File file : files) { if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden() && (files.length > 0)) { // it is a real directory (not a USB drive)... if (file.toString().equals(primaryExternalStorage.toString())) data.add(file); else { if (!file.toString().contains("usb") || !file.toString().contains("USB")) { data.add(file); } } } } return data.toArray(new File[data.size()]); } } else { // we cannot read the External Storage..., return null return null; } }
From source file:com.grapeshot.halfnes.FileUtils.java
public static boolean exists(final String path) { File f = new File(path); return f.canRead() && !f.isDirectory(); }
From source file:com.rom.jmultipatcher.Utils.java
public static void checkFilePermissions(final String filepath, final boolean canRead, final boolean canWrite) { final Path path = Paths.get(filepath); final File file = path.toFile(); if (canRead && !file.canRead()) { throw new IllegalArgumentException("Can't read file" + filepath); }//from w w w . j a v a 2s .c om if (canWrite && !file.canWrite()) { throw new IllegalArgumentException("Can't write to file" + filepath); } }
From source file:luceneprueba.utils.FileParser.java
static public void createFilesFromJSONArray() throws IOException { File dir = new File("files/input"); File[] files = dir.listFiles(); if (files == null) { System.out.println("No existe la carpeta \'input\' dentro de la carpeta files."); return;//from w w w . j a v a 2 s. c om } if (files.length == 0) { System.out.println("No hay ningun archivo en la carpeta \'input\' para ser indexado"); return; } BufferedReader br; String fileContent; JSONArray jsonArray = null; JSONParser jsonParser = new JSONParser(); int i = 1; FileWriter datosReviews = null; try { datosReviews = new FileWriter("files/output/datos_reviews.txt"); } catch (IOException ex) { ex.printStackTrace(System.out); } for (File file : files) { if (file.isFile() && file.canRead() && file.getName().endsWith(".txt")) { System.out.println("Leyendo el archivo: " + file.getName()); FileWriter contentReviews; try { br = new BufferedReader(new FileReader(file)); fileContent = br.readLine(); jsonArray = (JSONArray) jsonParser.parse(fileContent); Iterator it = jsonArray.iterator(); DecimalFormat formato = new DecimalFormat("000000"); while (it.hasNext()) { JSONObject json = (JSONObject) it.next(); if (json.get("Genre") != null && json.get("Date") != null) { contentReviews = new FileWriter( "files/output/clasificador/review_clasificador_" + formato.format(i) + ".txt"); datosReviews.write(Integer.toString(i) + "_" + (String) json.get("Date") + "_" + (String) json.get("Genre") + "_" + (String) json.get("Score") + "\n"); contentReviews.write(Integer.toString(i) + " " + (String) json.get("Review")); i++; contentReviews.close(); } } br.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(System.out); } catch (IOException | ParseException ex) { ex.printStackTrace(System.out); } } } datosReviews.close(); }
From source file:com.hp.mqm.atrf.Main.java
private static void configureLog4J() { //set process Id on local RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean(); String pid = rt.getName();//from w w w . j av a 2s . c o m ThreadContext.put("PID", pid); String log4jConfiguration = System.getProperty("log4j.configuration"); if (StringUtils.isEmpty(log4jConfiguration)) { //try to take from file File f = new File("log4j2.xml"); URI uri = null; if (f.exists() && !f.isDirectory() && f.canRead()) { uri = f.toURI(); } else { //take it from resources try { uri = Main.class.getClassLoader().getResource("log4j2.xml").toURI(); } catch (URISyntaxException e) { logger.info("Failed to load Log4j configuration from resource file"); } } LoggerContext context = (LoggerContext) LogManager.getContext(false); context.setConfigLocation(uri); //logger.info("Log4j configuration loaded from " + uri.toString()); } else { logger.info("Log4j configuration is loading from log4j.configuration=" + log4jConfiguration); } }