Guava (GOOGLE Common Libraares) input/output framework input/output framework introduction
Guava is a core module of Google Common Libraries. It provides many powerful input/output framework functions, which is convenient for Java developers to process input and output operations.This article will introduce the key functions of the Guava input/output framework and explain their usage through the Java code example.
1. File operation:
The Files class in the Guava provides simplified file operation functions.It can easily read and write files, as well as copying, moving and deleting files.
Example of reading files:
String content = Files.asCharSource(new File("example.txt"), Charsets.UTF_8).read();
System.out.println(content);
Example of writing files:
String content = "Hello, Guava!";
Files.asCharSink(new File("example.txt"), Charsets.UTF_8).write(content);
2. Byte flow processing:
InputSupplier and OUTPUTSUPPLIER interface provides convenient tools for processing byte running.They can effectively manage the opening and closing of resources, and simplify the use of input and output streams.
Example of reading bytes:
InputSupplier<FileInputStream> inputSupplier = Files.newInputStreamSupplier(new File("example.txt"));
InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(CharStreams.newInputStreamSupplier(new File("example.txt")), Charsets.UTF_8);
String content = CharStreams.toString(readerSupplier.get());
System.out.println(content);
Write intelum sample:
OutputSupplier<FileOutputStream> outputSupplier = Files.newOutputStreamSupplier(new File("example.txt"));
OutputSupplier<OutputStreamWriter> writerSupplier = CharStreams.newWriterSupplier(CharStreams.newOutputStreamSupplier(new File("example.txt")), Charsets.UTF_8);
String content = "Hello, Guava!";
CharStreams.write(content, writerSupplier.get());
3. Byte buffer:
Guava provides a Bytestreams class that contains practical methods for byte buffer.These methods can efficiently process a large amount of byte data.
Byte buffer example:
byte[] buffer = new byte[1024];
InputStream inputStream = new FileInputStream("example.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = ByteStreams.read(inputStream, buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, bytesRead);
}
byte[] data = baos.toByteArray();
4. Character flow processing:
The GUAVA's Charstreams class provides some practical methods for character stream operations.It can convert character string, read character streams one by one.
Switching of character flow processing:
InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(new File("example.txt"), Charsets.UTF_8);
String content = CharStreams.toString(readerSupplier.get());
System.out.println(content);
InputSupplier<BufferedReader> bufferedReaderSupplier = CharStreams.newReaderSupplier(new File("example.txt"), Charsets.UTF_8);
List<String> lines = CharStreams.readLines(bufferedReaderSupplier.get());
for (String line : lines) {
System.out.println(line);
}
In summary, the GUAVA's input/output framework provides many practical functions that simplify the input and output operation.Whether it is file operation, byte flow processing, or character stream processing, developers can easily handle the input and output operations through the class and methods provided by the Guava library.