MCPcopy Index your code
hub / github.com/antlr/codebuff / Files

Class Files

corpus/java/training/guava/io/Files.java:65–856  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected