List of usage examples for java.io IOException IOException
public IOException(Throwable cause)
From source file:Main.java
private static byte[] getSHA1Digest(String data) throws IOException { byte[] bytes = null; try {/*from ww w . j a v a 2 s .c o m*/ MessageDigest md = MessageDigest.getInstance("SHA-1"); bytes = md.digest(data.getBytes("UTF-8")); } catch (GeneralSecurityException gse) { throw new IOException(gse); } return bytes; }
From source file:Main.java
/** * Determine whether a file is a ZIP File. *///from w w w . j ava 2 s . c o m public static boolean isZipFile(File file) throws IOException { if (file.isDirectory()) { return false; } if (!file.canRead()) { throw new IOException("Cannot read file " + file.getAbsolutePath()); } if (file.length() < 4) { return false; } DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); int test = in.readInt(); in.close(); return test == 0x504b0304; }
From source file:Main.java
static public String prepareFilePath(String fileName, String dir) throws IOException { File dictionaryRoot = new File(Environment.getExternalStorageDirectory(), dir); File dictionaryDirFile = new File(dictionaryRoot, fileName.substring(0, 1)); if (!dictionaryDirFile.exists()) { if (!dictionaryDirFile.mkdirs()) { throw new IOException("Cannot create directory: " + dictionaryDirFile); }/*from ww w . j av a 2 s. co m*/ } return new File(dictionaryDirFile, fileName + ".mp3").getCanonicalPath(); }
From source file:Main.java
public static void writeFile(File file, byte[] content) throws IOException { if (!file.exists()) { try {//from w w w .jav a 2 s . co m file.createNewFile(); } catch (IOException e) { throw new IOException("not crete file=" + file.getAbsolutePath()); } } FileOutputStream fileOutputStream = null; ByteArrayInputStream bis = null; try { bis = new ByteArrayInputStream(content); fileOutputStream = new FileOutputStream(file, false); byte[] buffer = new byte[1024]; int length = 0; while ((length = bis.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, length); } fileOutputStream.flush(); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } if (bis != null) { bis.close(); } } }
From source file:Main.java
public static byte[] readBytes(File file) throws IOException { //check/*from w w w . ja v a 2 s .co m*/ if (!file.exists()) { throw new FileNotFoundException("File not exist: " + file); } if (!file.isFile()) { throw new IOException("Not a file:" + file); } long len = file.length(); if (len >= Integer.MAX_VALUE) { throw new IOException("File is larger then max array size"); } byte[] bytes = new byte[(int) len]; FileInputStream in = null; try { in = new FileInputStream(file); in.read(bytes); } finally { close(in); } return bytes; }
From source file:Main.java
public static byte[] readFile(File file) throws IOException { // Open file//from w ww.j a va 2 s . c o m RandomAccessFile f = new RandomAccessFile(file, "r"); try { // Get and check length long longlength = f.length(); int length = (int) longlength; if (length != longlength) throw new IOException("File size >= 2 GB"); // Read file and return data byte[] data = new byte[length]; f.readFully(data); return data; } finally { f.close(); } }
From source file:Main.java
/** * Load a DOM document//from ww w . ja v a 2s.com */ public static Document loadDocument(InputStream in) throws IOException { try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(in); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage()); } catch (SAXException e) { throw new IOException(e.getMessage()); } }
From source file:Main.java
/** * Decodes HEX representation of the string. * * @param data HEX//from w ww. j a v a 2 s. com * @return String representation of the {@code data} * @throws IOException if some error occurs */ private static String decodeHex(String data) throws IOException { if (data == null || data.isEmpty()) { return data; } if (data.length() % 2 != 0) { throw new IOException("String is not in hexadecimal representation."); } byte[] bytes = new byte[data.length() / 2]; for (int i = 0; i < data.length(); i += 2) { bytes[i / 2] = Integer.valueOf(data.substring(i, i + 2), 16).byteValue(); } return new String(bytes, "UTF-8"); }
From source file:Utils.java
/** * delete all files under this file and including this file * /*from w w w. j a v a2s.c o m*/ * @param f * @throws IOException */ public static void deleteAll(File f) throws IOException { recurse(f, new RecurseAction() { public void doFile(File file) throws IOException { file.delete(); if (file.exists()) { throw new IOException("Failed to delete " + file.getPath()); } } public void doBeforeFile(File f) { } public void doAfterFile(File f) { } }); }
From source file:Main.java
public static void unzip(InputStream is, String dir) throws IOException { File dest = new File(dir); if (!dest.exists()) { dest.mkdirs();/* www . j a va2s . c o m*/ } if (!dest.isDirectory()) throw new IOException("Invalid Unzip destination " + dest); if (null == is) { throw new IOException("InputStream is null"); } ZipInputStream zip = new ZipInputStream(is); ZipEntry ze; while ((ze = zip.getNextEntry()) != null) { final String path = dest.getAbsolutePath() + File.separator + ze.getName(); String zeName = ze.getName(); char cTail = zeName.charAt(zeName.length() - 1); if (cTail == File.separatorChar) { File file = new File(path); if (!file.exists()) { if (!file.mkdirs()) { throw new IOException("Unable to create folder " + file); } } continue; } FileOutputStream fout = new FileOutputStream(path); byte[] bytes = new byte[1024]; int c; while ((c = zip.read(bytes)) != -1) { fout.write(bytes, 0, c); } zip.closeEntry(); fout.close(); } }