List of usage examples for java.io IOException IOException
public IOException()
From source file:Main.java
public static void main(String[] argv) { Logger logger = Logger.getLogger("com.mycompany.MyClass"); try {/*from w ww . j a va2s . c o m*/ throw new IOException(); } catch (Throwable e) { logger.log(Level.SEVERE, "Uncaught exception", e); } Exception ex = new IllegalStateException(); logger.throwing("Main class", "myMethod", ex); }
From source file:com.athena.peacock.agent.Starter.java
/** * <pre>//ww w .j a va 2 s. c o m * * </pre> * @param args */ @SuppressWarnings("resource") public static void main(String[] args) { int rand = (int) (Math.random() * 100) % 50; System.setProperty("random.seconds", Integer.toString(rand)); String configFile = null; try { configFile = PropertyUtil.getProperty(PeacockConstant.CONFIG_FILE_KEY); } catch (Exception e) { // nothing to do. } finally { if (StringUtils.isEmpty(configFile)) { configFile = "/peacock/agent/config/agent.conf"; } } /** * ${peacock.agent.config.file.name} ?? load ? ?? ? ? ? . */ String errorMsg = "\n\"" + configFile + "\" file does not exist or cannot read.\n" + "Please check \"" + configFile + "\" file exists and can read."; Assert.isTrue(AgentConfigUtil.exception == null, errorMsg); Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_IP), "ServerIP cannot be empty."); Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_PORT), "ServerPort cannot be empty."); /** * Agent ID ?? ${peacock.agent.agent.file.name} ? ?, * ?? ? Agent ID ? ?? . */ String agentFile = null; String agentId = null; try { agentFile = PropertyUtil.getProperty(PeacockConstant.AGENT_ID_FILE_KEY); } catch (Exception e) { // nothing to do. } finally { if (StringUtils.isEmpty(agentFile)) { agentFile = "/peacock/agent/.agent"; } } File file = new File(agentFile); boolean isNew = false; if (file.exists()) { try { agentId = IOUtils.toString(file.toURI()); // ? ? agent ID agent ID? ? 36? ? ?. if (StringUtils.isEmpty(agentId) || agentId.length() != 36) { throw new IOException(); } } catch (IOException e) { logger.error(agentFile + " file cannot read or saved invalid agent ID.", e); agentId = PeacockAgentIDGenerator.generateId(); isNew = true; } } else { agentId = PeacockAgentIDGenerator.generateId(); isNew = true; } if (isNew) { logger.info("New Agent-ID({}) be generated.", agentId); try { file.setWritable(true); OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file)); output.write(agentId); file.setReadOnly(); IOUtils.closeQuietly(output); } catch (UnsupportedEncodingException e) { logger.error("UnsupportedEncodingException has occurred : ", e); } catch (FileNotFoundException e) { logger.error("FileNotFoundException has occurred : ", e); } catch (IOException e) { logger.error("IOException has occurred : ", e); } } // Spring Application Context Loading logger.debug("Starting application context..."); AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/context-*.xml"); applicationContext.registerShutdownHook(); }
From source file:Main.java
public static void deleteIfExists(File file) throws IOException { if (file.exists() && !file.delete()) { throw new IOException(); }/* w w w . j a va2 s .com*/ }
From source file:Main.java
public static Document readXMLFile(String filename) throws IOException { DocumentBuilder builder;//from ww w . j ava 2 s . c om DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { builder = factory.newDocumentBuilder(); Document document = builder.parse(filename); return document; } catch (Exception e) { throw new IOException(); } }
From source file:Main.java
public static String readFileToString(final Class contextClass, final String streamIdentifier) throws IOException { InputStreamReader inputStreamReader = null; try {//from www . j a v a2 s. com inputStreamReader = new InputStreamReader(contextClass.getResourceAsStream(streamIdentifier)); return CharStreams.toString(inputStreamReader); } catch (IOException e) { throw new IOException(); } finally { if (inputStreamReader != null) { inputStreamReader.close(); } } }
From source file:Main.java
public static final void writeFix(OutputStream out, String s, int fix) throws IOException { if (fix <= 0) return;//from w w w . j av a2s . c om byte[] b = s.getBytes(); if (b.length < fix) throw new IOException(); out.write(b, 0, fix); }
From source file:Main.java
public static void writeXMLFile(Node node, String filename) throws IOException { try {//from ww w . j av a2s . co m // Prepare the DOM document for writing DOMSource source = new DOMSource(node); // Prepare the output file StreamResult result = new StreamResult(filename); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException e) { throw new IOException(); } catch (TransformerException e) { throw new IOException(); } }
From source file:com.uksf.mf.core.utility.ClassNames.java
/** * Gets class names from file on uksf server * @return map of class names: old, new//w w w. jav a 2 s.co m */ public static LinkedHashMap<String, String> getClassNames() { LinkedHashMap<String, String> classNames = new LinkedHashMap<>(); try { URL url = new URL("http://www.uk-sf.com/mf/CLASSES.txt"); if (checkConnection(url)) { throw new IOException(); } InputStream stream = url.openStream(); List<String> lines = IOUtils.readLines(stream, "UTF-8"); for (String line : lines) { String parts[] = line.split(",", -1); if (parts[1] == null) parts[1] = ""; classNames.put(parts[0], parts[1]); } } catch (IOException e) { LogHandler.logSeverity(WARNING, "Cannot reach 'www.uk-sf.com', class name swap will not run"); return null; } return classNames; }
From source file:Main.java
/** * Read a number of bytes or throw./* w w w . j a va 2 s .c o m*/ * * @param size * The number of bytes read * @return The array of bytes read. * @throws IOException */ public static byte[] readBytes(DataInputStream stream, int size) throws IOException { byte[] buf = new byte[size]; int toRead = size; int done = 0; while (toRead > 0) { int read = stream.read(buf, done, toRead); if (read == -1) { throw new IOException(); } done += read; toRead -= read; } return buf; }
From source file:com.cprassoc.solr.auth.util.CommandLineExecutor.java
public static int execChain(ArrayList<String> cmds) { int exitValue = -1; try {//from w ww.j a v a 2 s .com for (int i = 0; i < cmds.size(); i++) { exitValue = exec(cmds.get(i)); if (exitValue > 0) { throw new IOException(); } } } catch (Exception e) { e.printStackTrace(); } return exitValue; }