List of usage examples for java.io IOException IOException
public IOException(Throwable cause)
From source file:com.sebuilder.interpreter.IO.java
/** * @param scriptO A JSONObject describing a script. * @return A script, ready to run.//w w w .j ava2 s . c o m * @throws IOException If anything goes wrong with interpreting the JSON. */ public static Script parse(JSONObject scriptO) throws IOException { try { if (!scriptO.get("seleniumVersion").equals("2")) { throw new IOException("Unsupported Selenium version: \"" + scriptO.get("seleniumVersion") + "\"."); } if (scriptO.getInt("formatVersion") != 1) { throw new IOException( "Unsupported Selenium script format version: \"" + scriptO.get("formatVersion") + "\"."); } Script script = new Script(); JSONArray stepsA = scriptO.getJSONArray("steps"); for (int i = 0; i < stepsA.length(); i++) { JSONObject stepO = stepsA.getJSONObject(i); Script.Step step = new Script.Step(getStepTypeOfName(stepO.getString("type"))); step.negated = stepO.optBoolean("negated", false); script.steps.add(step); JSONArray keysA = stepO.names(); for (int j = 0; j < keysA.length(); j++) { String key = keysA.getString(j); if (key.equals("type") || key.equals("negated")) { continue; } if (stepO.optJSONObject(key) != null) { step.locatorParams.put(key, new Locator(stepO.getJSONObject(key).getString("type"), stepO.getJSONObject(key).getString("value"))); } else { step.stringParams.put(key, stepO.getString(key)); } } } return script; } catch (Exception e) { throw new IOException("Could not parse script.", e); } }
From source file:de.flapdoodle.embedmongo.Files.java
public static File createOrCheckDir(String dir) throws IOException { File tempFile = new File(dir); if ((tempFile.exists()) && (tempFile.isDirectory())) return tempFile; if (!tempFile.mkdir()) throw new IOException("Could not create Tempdir: " + tempFile); return tempFile; }
From source file:com.zb.app.common.file.FileUtils.java
public static void unJar(File jarFile, File toDir) throws IOException { JarFile jar = new JarFile(jarFile); try {//from ww w . j a v a2 s.co m Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); if (!entry.isDirectory()) { InputStream in = jar.getInputStream(entry); try { File file = new File(toDir, entry.getName()); if (!file.getParentFile().mkdirs()) { if (!file.getParentFile().isDirectory()) { throw new IOException("Mkdirs failed to create " + file.getParentFile().toString()); } } OutputStream out = new FileOutputStream(file); try { byte[] buffer = new byte[8192]; int i; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { out.close(); } } finally { in.close(); } } } } finally { jar.close(); } }
From source file:edu.kit.cockpit.valuationserver.sfmpersistency.FileUtil.java
/** * checks file for reading//from w ww . j ava 2 s . c om * * @param modelFile * @param modelFileName */ public static void checkFileForReading(File file) throws IOException { if (!file.exists()) { String error = "File '" + file.getPath() + "' does not exist"; log.error(error); throw new IOException(error); } }
From source file:com.lenovo.tensorhusky.common.utils.ProcessIdFileReader.java
/** * Get the process id from specified file path. Parses each line to find a * valid number and returns the first one found. * * @return Process Id if obtained from path specified else null * @throws IOException/* w w w .j av a2 s . c o m*/ */ public static String getProcessId(Path path, boolean check) throws IOException { if (path == null) { throw new IOException("Trying to access process id from a null path"); } LOG.debug("Accessing pid from pid file " + path); String processId = null; BufferedReader bufReader = null; try { File file = new File(path.toString()); if (file.exists()) { FileInputStream fis = new FileInputStream(file); bufReader = new BufferedReader(new InputStreamReader(fis, "UTF-8")); while (true) { String line = bufReader.readLine(); if (line == null) { break; } String temp = line.trim(); if (!temp.isEmpty()) { if (Shell.WINDOWS) { // On Windows, pid is expected to be a container ID, so // find first // line that parses successfully as a container ID. try { processId = temp; break; } catch (Exception e) { // do nothing } } else { // Otherwise, find first line containing a numeric pid. if (check) { try { Long pid = Long.valueOf(temp); if (pid > 0) { processId = temp; break; } } catch (Exception e) { // do nothing } } else { processId = temp; } } } } } } finally { if (bufReader != null) { bufReader.close(); } } LOG.debug("Got pid " + (processId != null ? processId : "null") + " from path " + path); return processId; }
From source file:com.qwazr.webapps.transaction.ControllerManager.java
public synchronized static void load(File dataDir) throws IOException { if (INSTANCE != null) throw new IOException("Already loaded"); INSTANCE = new ControllerManager(dataDir); }
From source file:com.granita.contacticloudsync.log.ExternalFileLogger.java
public ExternalFileLogger(Context context, String fileName, boolean verbose) throws IOException { this.verbose = verbose; File dir = getDirectory(context); if (dir == null) throw new IOException("External media not available for log creation"); name = StringUtils.remove(StringUtils.remove(fileName, File.pathSeparatorChar), File.separatorChar); File log = new File(dir, name); writer = new PrintWriter(log); }
From source file:com.epam.spring.core.logger.FileEventLogger.java
public void init() throws IOException { System.out.println("Initializing FileEventLogger object."); this.file = new File(filename); if (!this.file.canWrite()) { throw new IOException("File " + filename + "is not writable."); }//from w w w . j a v a 2s .co m }
From source file:com.carmatech.maven.utils.MergeUtils.java
public static void savePropertiesTo(final File targetFile, final PropertiesConfiguration properties, final String comment) throws IOException { properties.setHeader(comment);/*w w w .ja va2 s . co m*/ try { Files.createParentDirs(targetFile); properties.save(targetFile); } catch (ConfigurationException e) { throw new IOException("Unable to save properties file " + targetFile); } }
From source file:net.dataforte.commons.resources.ClassUtils.java
/** * Returns all resources beneath a folder. Supports filesystem, JARs and JBoss VFS * //from w w w. j av a 2 s.c o m * @param folder * @return * @throws IOException */ public static URL[] getResources(String folder) throws IOException { List<URL> urls = new ArrayList<URL>(); ArrayList<File> directories = new ArrayList<File>(); try { ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) { throw new IOException("Can't get class loader."); } // Ask for all resources for the path Enumeration<URL> resources = cld.getResources(folder); while (resources.hasMoreElements()) { URL res = resources.nextElement(); String resProtocol = res.getProtocol(); if (resProtocol.equalsIgnoreCase("jar")) { JarURLConnection conn = (JarURLConnection) res.openConnection(); JarFile jar = conn.getJarFile(); for (JarEntry e : Collections.list(jar.entries())) { if (e.getName().startsWith(folder) && !e.getName().endsWith("/")) { urls.add(new URL( joinUrl(res.toString(), "/" + e.getName().substring(folder.length() + 1)))); // FIXME: fully qualified name } } } else if (resProtocol.equalsIgnoreCase("vfszip") || resProtocol.equalsIgnoreCase("vfs")) { // JBoss 5+ try { Object content = res.getContent(); Method getChildren = content.getClass().getMethod("getChildren"); List<?> files = (List<?>) getChildren.invoke(res.getContent()); Method toUrl = null; for (Object o : files) { if (toUrl == null) { toUrl = o.getClass().getMethod("toURL"); } urls.add((URL) toUrl.invoke(o)); } } catch (Exception e) { throw new IOException("Error while scanning " + res.toString(), e); } } else if (resProtocol.equalsIgnoreCase("file")) { directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8"))); } else { throw new IOException("Unknown protocol for resource: " + res.toString()); } } } catch (NullPointerException x) { throw new IOException(folder + " does not appear to be a valid folder (Null pointer exception)"); } catch (UnsupportedEncodingException encex) { throw new IOException(folder + " does not appear to be a valid folder (Unsupported encoding)"); } // For every directory identified capture all the .class files for (File directory : directories) { if (directory.exists()) { // Get the list of the files contained in the package String[] files = directory.list(); for (String file : files) { urls.add(new URL("file:///" + joinPath(directory.getAbsolutePath(), file))); } } else { throw new IOException( folder + " (" + directory.getPath() + ") does not appear to be a valid folder"); } } URL[] urlsA = new URL[urls.size()]; urls.toArray(urlsA); return urlsA; }