/** * Reads the contents of a file into a String. * The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the file contents, never {@code null} * @throws IOException in case of an I/O error * @since 2.3 */ public static String readFileToString(final File file, final Charset encoding) throws IOException { try (InputStream in = openInputStream(file)) { return IOUtils.toString(in, Charsets.toCharset(encoding)); } }
/** * Reads the contents of a file line by line to a List of Strings. * The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the list of Strings representing each line in the file, never {@code null} * @throws IOException in case of an I/O error * @since 2.3 */ public static List<String> readLines(final File file, final Charset encoding) throws IOException { try (InputStream in = openInputStream(file)) { return IOUtils.readLines(in, Charsets.toCharset(encoding)); } }
/** * Reads the contents of a file into a String. The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the file contents, never {@code null} * @throws IOException in case of an I/O error * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported. * @since 2.3 */ public static String readFileToString(final File file, final String encoding) throws IOException { return readFileToString(file, Charsets.toCharset(encoding)); }
/** * Reads the contents of a file line by line to a List of Strings. The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the list of Strings representing each line in the file, never {@code null} * @throws IOException in case of an I/O error * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported. * @since 1.1 */ public static List<String> readLines(final File file, final String encoding) throws IOException { return readLines(file, Charsets.toCharset(encoding)); }
/** * Creates a test file with a specified number of lines. * * @param file target file * @param lineCount number of lines to create * * @throws IOException If an I/O error occurs */ private List<String> createLinesFile(final File file, final int lineCount) throws IOException { final List<String> lines = createStringLines(lineCount); FileUtils.writeLines(file, lines); return lines; }
/** * Returns the next line in the wrapped <code>Reader</code>. * * @return the next line from the input * @throws NoSuchElementException if there is no line to return */ @Override public String next() { return nextLine(); }
@Override public boolean matches(final String className) { return FilenameUtils.wildcardMatch(className, pattern); } }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the file is null */ public void track(final File file, final Object marker) { track(file, marker, null); }
/** Construct a new Reaper */ Reaper() { super("File Reaper"); setPriority(Thread.MAX_PRIORITY); setDaemon(true); }
/** * Delegates to {@link EndianUtils#readSwappedUnsignedShort(InputStream)}. * @return the read long * @throws EOFException if an end of file is reached unexpectedly * @throws IOException if an I/O error occurs */ @Override public int readUnsignedShort() throws IOException, EOFException { return EndianUtils.readSwappedUnsignedShort( in ); }
/** * Delegates to {@link EndianUtils#readSwappedShort(InputStream)}. * @return the read long * @throws EOFException if an end of file is reached unexpectedly * @throws IOException if an I/O error occurs */ @Override public short readShort() throws IOException, EOFException { return EndianUtils.readSwappedShort( in ); }
/** * Replaces the enumeration from the stream with a real one. * This ensures that the correct flag is set for SYSTEM. * * @return the resolved object */ private Object readResolve() { return forName(name); }
/** Handles Cancel. */ @Override protected void handleCancelled(final File startDirectory, final Collection<File> results, final CancelException cancel) throws IOException { if (!suppressCancel) { super.handleCancelled(startDirectory, results, cancel); } } }
/** * Tags any IOExceptions thrown, wrapping and re-throwing. * * @param e The IOException thrown * @throws IOException if an I/O error occurs */ @Override protected void handleIOException(final IOException e) throws IOException { throw new TaggedIOException(e, tag); }
/** * Counts the size of a directory recursively (sum of the length of all files). * * @param directory directory to inspect, must not be {@code null} * @return size of directory in bytes, 0 if directory is security restricted. * @throws NullPointerException if the directory is {@code null} * @since 2.4 */ public static BigInteger sizeOfDirectoryAsBigInteger(final File directory) { checkDirectory(directory); return sizeOfDirectoryBig0(directory); }
@Override protected FileCleaningTracker newInstance() { return FileCleaner.getInstance(); } }
/** * Creates a test file with a specified number of lines. * * @param file target file * @param encoding the encoding to use while writing the lines * @param lineCount number of lines to create * * @throws IOException If an I/O error occurs */ private List<String> createLinesFile(final File file, final String encoding, final int lineCount) throws IOException { final List<String> lines = createStringLines(lineCount); FileUtils.writeLines(file, encoding, lines); return lines; }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the path is null */ public void track(final String path, final Object marker) { track(path, marker, null); }
/** * Handles Cancel. */ @Override protected void handleCancelled(final File startDirectory, final Collection results, final CancelException cancel) throws IOException { if (!suppressCancel) { super.handleCancelled(startDirectory, results, cancel); } } }
/** * Tags any IOExceptions thrown, wrapping and re-throwing. * * @param e The IOException thrown * @throws IOException if an I/O error occurs */ @Override protected void handleIOException(final IOException e) throws IOException { throw new TaggedIOException(e, tag); }