Provides utility methods for working with files. All method parameters must be non-null unless documented otherwise. @author Chris Nokleberg @author Colin Decker @since 1.0
| 63 | |
| 64 | |
| 65 | @Beta |
| 66 | @GwtIncompatible |
| 67 | public final class Files { |
| 68 | |
| 69 | /** Maximum loop count when creating temp directories. */ |
| 70 | private static final int TEMP_DIR_ATTEMPTS = 10000; |
| 71 | private Files() {} |
| 72 | |
| 73 | /** |
| 74 | * Returns a buffered reader that reads from a file using the given character set. |
| 75 | * |
| 76 | * @param file the file to read from |
| 77 | * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for |
| 78 | * helpful predefined constants |
| 79 | * @return the buffered reader |
| 80 | */ |
| 81 | |
| 82 | |
| 83 | public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException { |
| 84 | checkNotNull(file); |
| 85 | checkNotNull(charset); |
| 86 | return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns a buffered writer that writes to a file using the given character set. |
| 91 | * |
| 92 | * @param file the file to write to |
| 93 | * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for |
| 94 | * helpful predefined constants |
| 95 | * @return the buffered writer |
| 96 | */ |
| 97 | |
| 98 | |
| 99 | public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException { |
| 100 | checkNotNull(file); |
| 101 | checkNotNull(charset); |
| 102 | return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns a new {@link ByteSource} for reading bytes from the given file. |
| 107 | * |
| 108 | * @since 14.0 |
| 109 | */ |
| 110 | |
| 111 | |
| 112 | public static ByteSource asByteSource(File file) { |
| 113 | return new FileByteSource(file); |
| 114 | } |
| 115 | |
| 116 | private static final class FileByteSource extends ByteSource { |
| 117 | private final File file; |
| 118 | private FileByteSource(File file) { |
| 119 | this.file = checkNotNull(file); |
| 120 | } |
| 121 | |
| 122 | @Override |
nothing calls this directly
no outgoing calls
no test coverage detected