/** * Copies all bytes from a file to an output stream. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, OutputStream)}. * * @param from the source file * @param to the output stream * @throws IOException if an I/O error occurs */ public static void copy(File from, OutputStream to) throws IOException { asByteSource(from).copyTo(to); }
/** * Returns a new {@link CharSource} for reading character data from the given file using the given * character set. * * @since 14.0 */ public static CharSource asCharSource(File file, Charset charset) { return asByteSource(file).asCharSource(charset); }
/** * Computes the hash code of the {@code file} using {@code hashFunction}. * * @param file the file to read * @param hashFunction the hash function to use to hash the data * @return the {@link HashCode} of all of the bytes in the file * @throws IOException if an I/O error occurs * @since 12.0 * @deprecated Prefer {@code asByteSource(file).hash(hashFunction)}. This method is scheduled to * be removed in January 2019. */ @Deprecated public static HashCode hash(File file, HashFunction hashFunction) throws IOException { return asByteSource(file).hash(hashFunction); }
/** * Reads all bytes from a file into a byte array. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}. * * @param file the file to read from * @return a byte array containing all the bytes from file * @throws IllegalArgumentException if the file is bigger than the largest possible byte array * (2^31 - 1) * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(File file) throws IOException { return asByteSource(file).read(); }
/** * Returns true if the given files exist, are not directories, and contain the same bytes. * * @throws IOException if an I/O error occurs */ public static boolean equal(File file1, File file2) throws IOException { checkNotNull(file1); checkNotNull(file2); if (file1 == file2 || file1.equals(file2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ long len1 = file1.length(); long len2 = file2.length(); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return asByteSource(file1).contentEquals(asByteSource(file2)); }
/** * Process the bytes of a file. * * <p>(If this seems too complicated, maybe you're looking for {@link #toByteArray}.) * * @param file the file to read * @param processor the object to which the bytes of the file are passed. * @return the result of the byte processor * @throws IOException if an I/O error occurs * @deprecated Prefer {@code asByteSource(file).read(processor)}. This method is scheduled to be * removed in January 2019. */ @Deprecated @CanIgnoreReturnValue // some processors won't return a useful result public static <T> T readBytes(File file, ByteProcessor<T> processor) throws IOException { return asByteSource(file).read(processor); }
/** * Copies all the bytes from one file to another. * * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you * need to guard against those conditions, you should employ other file-level synchronization. * * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i> * file, the contents of that file will be deleted. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}. * * @param from the source file * @param to the destination file * @throws IOException if an I/O error occurs * @throws IllegalArgumentException if {@code from.equals(to)} */ public static void copy(File from, File to) throws IOException { checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); asByteSource(from).copyTo(asByteSink(to)); }