List of usage examples for java.io File length
public long length()
From source file:com.a544jh.kanamemory.io.JsonFileReader.java
protected static JSONObject readFileToJsonObject(File file) throws FileNotFoundException, JSONException { JSONObject jo;/* w ww . j a va 2s.co m*/ StringBuilder sb = new StringBuilder((int) file.length()); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { sb.append(scanner.nextLine()); } } jo = new JSONObject(sb.toString()); return jo; }
From source file:com.blockwithme.longdb.tools.Utils.java
/** Attempts to read file with name - 'OriginalFileName'.crc32 if present : * Assumes that the content inside 'OriginalFileName'.crc32 file is equal to * the CRC32 value of the Original file. Calculates CRC32 value of the * current file and Compares it with the original value. In case if the * .crc32 file is not found, or the file size is not 8 bytes this method * returns false, If the file is found but the values don't match this * method throws a RuntimeExcepiton.// ww w . j a v a 2s. com * * @param theFile * the file * @param isCompressed * true if compressed * @return true, if checksum successful * @throws Exception */ // TODO: When do we need to differentiate the "no crc file" and // "bad crc" cases? Is it not a failure, one way or another? public static boolean checkSum(final File theFile, final boolean isCompressed) throws Exception { final File crcFile = new File(theFile.getAbsolutePath() + ".crc32"); if (!crcFile.exists() || crcFile.length() != INT_BYTES * 2) { return false; } final String crcCode = FileUtils.readFileToString(crcFile); final long expectedCRC = Long.valueOf(crcCode, 16); // $codepro.audit.disable // handleNumericParsingErrors if (getCRC32(theFile, isCompressed) != expectedCRC) throw new RuntimeException("CRC-32 validation failed for file:" + theFile.getAbsolutePath()); return true; }
From source file:Main.java
public static byte[] readFileToMemory(File file) throws IOException { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); byte[] data = new byte[(int) file.length()]; int start = 0; int read = 0; while ((read = bis.read(data, start, data.length - start)) > 0) { start += read;/* w w w. j a v a 2 s.c o m*/ } bis.close(); fis.close(); return data; }
From source file:com.doculibre.constellio.utils.FileSizeUtils.java
public static String formatSize(File file, int decimalPos) { long fileSize; if (file.isDirectory()) { fileSize = FileUtils.sizeOfDirectory(file); } else {/*from ww w.j av a2s.c om*/ fileSize = file.length(); } return formatSize(fileSize, decimalPos); }
From source file:net.chris54721.infinitycubed.utils.Utils.java
public static boolean copy(File source, File target) { try {//from w w w .ja va2s . c o m Files.copy(source, target); return target.length() == source.length(); } catch (Exception e) { LogHelper.error("Failed copying file: " + e.getMessage(), e); return false; } }
From source file:S3ClientSideEncryptionWithSymmetricMasterKey.java
public static SecretKey loadSymmetricAESKey(String path, String algorithm) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // Read private key from file. File keyFile = new File(path + "/" + keyName); FileInputStream keyfis = new FileInputStream(keyFile); byte[] encodedPrivateKey = new byte[(int) keyFile.length()]; keyfis.read(encodedPrivateKey);// w w w . j a v a 2 s. co m keyfis.close(); // Generate secret key. return new SecretKeySpec(encodedPrivateKey, "AES"); }
From source file:Utils.java
public static byte[] readAsByteArray(String fileName) { File file = new File(fileName); return readAsByteArray(file, file.length()); }
From source file:Util.java
/** * Read bytes from a File into a byte[].//from ww w .j a va 2 s .c om * * @param file The File to read. * @return A byte[] containing the contents of the File. * @throws IOException Thrown if the File is too long to read or couldn't be * read fully. */ public static byte[] readBytesFromFile(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) { throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")"); } // 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:gov.nasa.arc.geocam.talk.UIUtils.java
/** * Play the audio of a {@link GeoCamTalkMessage} if available. * * @param context The activity context to send the intent from. * @param msg The {@link GeoCamTalkMessage} to play audio of. * @param player An instance of an {@link IAudioPlayer} * @param siteAuth The {@link ISiteAuth} to use to get attain audio from if not local * @throws ClientProtocolException the client protocol exception * @throws AuthenticationFailedException the authentication failed exception * @throws IOException Signals that an I/O exception has occurred. *//*from w w w. j av a 2 s . c o m*/ public static void playAudio(Context context, GeoCamTalkMessage msg, IAudioPlayer player, ISiteAuth siteAuth) throws ClientProtocolException, AuthenticationFailedException, IOException { String audioUrl = msg.getAudioUrl(); // No audio recorded with message if (msg.getAudio() == null && audioUrl == null) { return; } // We have audio, but not locally else if (msg.getAudio() == null && audioUrl != null) { String localFileName = siteAuth.getAudioFile(audioUrl, null); player.startPlaying(localFileName); File audioFile = new File(localFileName); int length = (int) audioFile.length(); byte[] audioBytes = new byte[length]; FileInputStream fis = new FileInputStream(audioFile); fis.read(audioBytes, 0, length); msg.setAudio(audioBytes); } player.startPlaying(msg.getAudio()); }
From source file:AnnotationClient.java
protected static String readFileAsString(File file) throws IOException { byte[] buffer = new byte[(int) file.length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(file)); f.read(buffer);// ww w. j a v a 2 s . co m return new String(buffer); }