List of usage examples for java.io File length
public long length()
From source file:Main.java
public static KeyPair loadKeyPair(Context context, String name) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { // Read Public Key. File filePublicKey = new File(context.getFilesDir(), name + "_public.key"); FileInputStream fis = new FileInputStream(filePublicKey); byte[] encodedPublicKey = new byte[(int) filePublicKey.length()]; fis.read(encodedPublicKey);// w ww . j a v a 2s . c o m fis.close(); // Read Private Key. File filePrivateKey = new File(context.getFilesDir(), name + "_private.key"); fis = new FileInputStream(filePrivateKey); byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()]; fis.read(encodedPrivateKey); fis.close(); // Generate KeyPair. KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); }
From source file:Main.java
public static int fileSize(String dir, String fileName) { File sdLien = Environment.getExternalStorageDirectory(); File inFile = new File(sdLien + File.separator + dir + File.separator + fileName); Log.d(TAG, "path of file : " + inFile); if (!inFile.exists()) { throw new RuntimeException("File doesn't exist"); }/*w w w. j a va2s . co m*/ return ((int) inFile.length()); }
From source file:Main.java
public static boolean isWAVFile(String fileName) { byte header[] = new byte[16]; try {//from w w w. j a v a 2 s . c om File f = new File(fileName); if (!f.exists()) { Log.d("OpusTool", fileName + ":" + "File does not exist."); return false; } long actualLength = f.length(); FileInputStream io = new FileInputStream(f); io.read(header, 0, 16); io.close(); String tag = new String(header, 0, 4) + new String(header, 8, 8); if (!tag.equals("RIFFWAVEfmt ")) { Log.d("OpusTool", fileName + ":" + "It's not a WAV file!"); return false; } long paraLength = (header[4] & 0x000000ff) | ((header[5] << 8) & 0x0000ff00) | ((header[6] << 16) & 0x00ff0000) | ((header[7] << 24) & 0xff000000); if (paraLength != actualLength - 8) { Log.d("OpusTool", fileName + ":" + "It might be a WAV file, but it's corrupted!"); return false; } return true; } catch (Exception e) { Log.d("OpusTool", fileName + ":" + "File Error"); return false; } }
From source file:Main.java
public static String[] getAvailableSchedulers() { File iofile = new File("/sys/block/mmcblk0/queue/scheduler"); String s = ""; FileInputStream fin2 = null;//from w w w . ja v a2s . c om try { fin2 = new FileInputStream(iofile); byte fileContent[] = new byte[(int) iofile.length()]; fin2.read(fileContent); s = new String(fileContent).trim().split("\n")[0]; } catch (FileNotFoundException e) { //System.out.println("File not found" + e); } catch (IOException ioe) { //System.out.println("Exception while reading file " + ioe); } finally { try { if (fin2 != null) { fin2.close(); } } catch (IOException ioe) { //System.out.println("Error while closing stream: " + ioe); } } String[] IOSchedulers = s.replace("[", "").replace("]", "").split(" "); return IOSchedulers; }
From source file:com.redsqirl.workflow.utils.FileStream.java
public static byte[] encryptFile(File in) throws Exception { int blockSize = 8; //Figure out how many bytes are padded int paddedCount = blockSize - ((int) in.length() % blockSize); //Figure out full size including padding int padded = (int) in.length() + paddedCount; byte[] decData = new byte[padded]; FileInputStream inStream = new FileInputStream(in); inStream.read(decData);// w ww. j a va 2 s . c o m inStream.close(); //Write out padding bytes as per PKCS5 algorithm for (int i = (int) in.length(); i < padded; ++i) { decData[i] = (byte) paddedCount; } return encrypt(decData); }
From source file:Main.java
public static String getCurrentScheduler() { File iofile = new File("/sys/block/mmcblk0/queue/scheduler"); String s = ""; FileInputStream fin2 = null;//from www . jav a2s . c o m try { fin2 = new FileInputStream(iofile); byte fileContent[] = new byte[(int) iofile.length()]; fin2.read(fileContent); s = new String(fileContent).trim().split("\n")[0]; } catch (FileNotFoundException e) { //System.out.println("File not found" + e); } catch (IOException ioe) { //System.out.println("Exception while reading file " + ioe); } finally { try { if (fin2 != null) { fin2.close(); } } catch (IOException ioe) { //System.out.println("Error while closing stream: " + ioe); } } int bropen = s.indexOf("["); int brclose = s.lastIndexOf("]"); return s.substring(bropen + 1, brclose); }
From source file:Main.java
public static void WriteTxtFile(Context context, String strcontent) { String strContent = strcontent + "\n"; try {// ww w .ja va2 s . c om File file = new File(context.getCacheDir() + File.separator + "wanjia.txt"); if (!file.exists()) { file.createNewFile(); } RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.seek(file.length()); raf.write(strContent.getBytes()); raf.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.intuit.cto.selfservice.service.Util.java
/** * Extract files from a package on the classpath into a directory. * @param packagePath e.g. "com/stuff" (always forward slash not backslash, never dot) * @param toDir directory to extract to//from w w w.j a va2 s. c o m * @return int the number of files copied * @throws java.io.IOException if something goes wrong, including if nothing was found on classpath */ public static int extractFromClasspathToFile(String packagePath, File toDir) throws IOException { String locationPattern = "classpath*:" + packagePath + "/**"; ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resourcePatternResolver.getResources(locationPattern); if (resources.length == 0) { throw new IOException("Nothing found at " + locationPattern); } int counter = 0; for (Resource resource : resources) { if (resource.isReadable()) { // Skip hidden or system files URL url = resource.getURL(); String path = url.toString(); if (!path.endsWith("/")) { // Skip directories int p = path.lastIndexOf(packagePath) + packagePath.length(); path = path.substring(p); File targetFile = new File(toDir, path); long len = resource.contentLength(); if (!targetFile.exists() || targetFile.length() != len) { // Only copy new files FileUtils.copyURLToFile(url, targetFile); counter++; } } } } logger.info("Unpacked {} files from {} to {}", new Object[] { counter, locationPattern, toDir }); return counter; }
From source file:net.sourceforge.jencrypt.lib.Utils.java
/** * Return the contents of a file as a byte array. * //from w w w.ja va2 s . c om * @param file * @return * @throws IOException */ public static byte[] getBytesFromFile(File file) throws IOException { if (file.length() > Integer.MAX_VALUE) { // File is too large throw new IOException("File '" + file.getName() + "' is bigger than Integer.MAX_VALUE (>2GB)"); } InputStream in = new FileInputStream(file); // Create the byte array to hold the data byte[] bytes = new byte[(int) file.length()]; int len = 0; // Reads bytes from the input stream and store them in buffer bytes len = in.read(bytes); // Ensure all the bytes have been read in if (len != bytes.length) { in.close(); throw new IOException("Could not completely read file " + file.getName()); } // Close the input stream and return bytes in.close(); return bytes; }
From source file:Main.java
/** * @return A map of all storage locations available *//*w w w . j av a 2s .c o m*/ public static Map<String, File> getAllStorageLocations() { Map<String, File> map = new HashMap<String, File>(10); List<String> mMounts = new ArrayList<String>(10); List<String> mVold = new ArrayList<String>(10); mMounts.add("/mnt/sdcard"); mVold.add("/mnt/sdcard"); try { File mountFile = new File("/proc/mounts"); if (mountFile.exists()) { Scanner scanner = new Scanner(mountFile); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.startsWith("/dev/block/vold/")) { String[] lineElements = line.split(" "); String element = lineElements[1]; // don't add the default mount path // it's already in the list. if (!element.equals("/mnt/sdcard")) mMounts.add(element); } } } } catch (Exception e) { e.printStackTrace(); } try { File voldFile = new File("/system/etc/vold.fstab"); if (voldFile.exists()) { Scanner scanner = new Scanner(voldFile); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.startsWith("dev_mount")) { String[] lineElements = line.split(" "); String element = lineElements[2]; if (element.contains(":")) element = element.substring(0, element.indexOf(":")); if (!element.equals("/mnt/sdcard")) mVold.add(element); } } } } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < mMounts.size(); i++) { String mount = mMounts.get(i); if (!mVold.contains(mount)) mMounts.remove(i--); } mVold.clear(); List<String> mountHash = new ArrayList<String>(10); for (String mount : mMounts) { File root = new File(mount); if (root.exists() && root.isDirectory() && root.canWrite()) { File[] list = root.listFiles(); String hash = "["; if (list != null) { for (File f : list) { hash += f.getName().hashCode() + ":" + f.length() + ", "; } } hash += "]"; if (!mountHash.contains(hash)) { String key = SD_CARD + "_" + map.size(); if (map.size() == 0) { key = SD_CARD; } else if (map.size() == 1) { key = EXTERNAL_SD_CARD; } mountHash.add(hash); map.put(key, root); } } } mMounts.clear(); if (map.isEmpty()) { map.put(SD_CARD, Environment.getExternalStorageDirectory()); } return map; }