List of usage examples for java.io File length
public long length()
From source file:com.redsqirl.workflow.utils.FileStream.java
public static byte[] decryptFile(File in) throws Exception { //Decrypt the file data: byte[] encData; //Read in the file: FileInputStream inStream = new FileInputStream(in); encData = new byte[(int) in.length()]; inStream.read(encData);/*from www. j a va2 s .c o m*/ inStream.close(); byte[] decData = decrypt(encData); //Figure out how much padding to remove int padCount = (int) decData[decData.length - 1]; //Naive check, will fail if plaintext file actually contained //this at the end //For robust check, check that padCount bytes at the end have same value if (padCount >= 1 && padCount <= 8) { decData = Arrays.copyOfRange(decData, 0, decData.length - padCount); } //Write the decrypted data to a new file: return decData; }
From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java
public static long getCRC32(File file) throws IOException { CheckedInputStream cis = null; long fileSize = 0; // Computer CRC32 checksum cis = new CheckedInputStream(new FileInputStream(file), new CRC32()); fileSize = file.length(); byte[] buf = new byte[128]; while (cis.read(buf) != -1) ;/*from w ww.ja v a 2s . c o m*/ long checksum = cis.getChecksum().getValue(); cis.close(); Logger.verbose("CRC32 of %s is %d", file.getPath(), checksum); return checksum; }
From source file:com.enonic.esl.io.FileUtil.java
/** * Returns the contents of the file in a byte array. * * @param file the file info/* ww w . ja v a2 s . c om*/ * @return a byte array containing the file's contents * @throws IOException */ public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }
From source file:com.mirth.connect.server.util.FileUtil.java
/** * Returns the contents of the file in a byte array. * /*from w w w . ja v a2 s . co m*/ * @deprecated * @see org.apache.commons.io.FileUtils#readFileToByteArray(File) * @param fileName * @return * @throws IOException */ public static byte[] readBytes(String fileName) throws IOException { File file = new File(fileName); InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large throw new IOException("File too large " + file.getName()); } // Create the byte array to hold the data byte[] bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }
From source file:cz.autoclient.league_of_legends.DataLoader.java
public static JSONObject fromFile(File file) throws FileNotFoundException { char[] buff = new char[(int) file.length()]; try {//w w w. j a v a2s . co m (new FileReader(file)).read(buff); } catch (IOException e) { throw new FileNotFoundException("Invalid file."); } String result = new StringBuilder().append(buff).toString(); //System.out.println("JSON from file:\n"+result); try { return new JSONObject(result); } catch (JSONException ex) { throw new FileNotFoundException("Invalid file contents - \n JSON error:" + ex); //Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ch.vorburger.mariadb4j.Util.java
/** * Extract files from a package on the classpath into a directory. * /*www . j a va2 s .c o m*/ * @param packagePath e.g. "com/stuff" (always forward slash not backslash, never dot) * @param toDir directory to extract to * @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 final URL url = resource.getURL(); String path = url.toString(); if (!path.endsWith("/")) { // Skip directories int p = path.lastIndexOf(packagePath) + packagePath.length(); path = path.substring(p); final File targetFile = new File(toDir, path); long len = resource.contentLength(); if (!targetFile.exists() || targetFile.length() != len) { // Only copy new files tryN(5, 500, new Procedure<IOException>() { @Override public void apply() throws IOException { FileUtils.copyURLToFile(url, targetFile); } }); counter++; } } } } if (counter > 0) { Object[] info = new Object[] { counter, locationPattern, toDir }; logger.info("Unpacked {} files from {} to {}", info); } return counter; }
From source file:msec.org.TarUtil.java
private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception { TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName()); entry.setSize(file.length()); taos.putArchiveEntry(entry);//w ww . j a va2 s .c om BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int count; byte data[] = new byte[BUFFERSZ]; while ((count = bis.read(data, 0, BUFFERSZ)) != -1) { taos.write(data, 0, count); } bis.close(); taos.closeArchiveEntry(); }
From source file:Main.java
public static double getSize(File file) { if (file.exists()) { if (!file.isFile()) { File[] fl = file.listFiles(); double ss = 0; for (File f : fl) { ss += getSize(f);/*w ww. j a va2 s. com*/ } return ss; } else { double ss = (double) file.length() / 1024 / 1024; return ss; } } else { return 0.0; } }
From source file:Main.java
public static double getDirUsageCapacity(String filePath) { File file = new File(filePath); if (!file.exists()) { return 0; }/*from w w w .ja v a2 s .c o m*/ if (file.isDirectory()) { double size = 0; for (File f : file.listFiles()) { size += getDirUsageCapacity(f.getPath()); } return size; } else { return file.length(); } }
From source file:Main.java
/** * Copies the contents of the file in {@code src} to {@code dst} and then deletes the {@code src} if copy was successful. * If the file copy was unsuccessful, the src file will not be deleted. * @param src Source file//w w w.j av a 2 s . c o m * @param dst Destination file * @throws IOException if an error occurred during the file copy */ static void moveFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { long bytesCopied = inChannel.transferTo(0, inChannel.size(), outChannel); if (bytesCopied >= src.length()) src.delete(); } finally { if (inChannel != null) inChannel.close(); outChannel.close(); } }