Guava (Google Common Libraares) input/output framework in the Java class library in the Java class library
Guava (Google Common Libraries) is a Java class library developed by Google. It provides many practical input/output framework so that developers can easily handle files, streams and other data sources.This article will introduce you to how to use the input/output framework of Guava and provide some Java code examples.
Guava's input/output framework includes the following important components:
1. Bytestreams: Guava provides more convenient ways to operate byte flow.For example, you can use the method to convert the input flow into byte array, or use the method to transform the input flow to byte array, or use the method to copy the input flow into the output stream with `Bytestream in, OutputStream Out).
import com.google.common.io.ByteStreams;
public class ByteStreamsExample {
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("input.txt");
byte[] byteArray = ByteStreams.toByteArray(inputStream);
OutputStream outputStream = new FileOutputStream("output.txt");
ByteStreams.copy(inputStream, outputStream);
}
}
2. Charstreams: Guava provides more convenient ways to operate the character flow.For example, you can use the `Charstreams.TOSTRING (Reader Reader) method to convert the character stream to a string, or use the method to use the` Charstreams.copy (Reader Reader, Writ Writer) method to copy the character flow into the writer.
import com.google.common.io.CharStreams;
public class CharStreamsExample {
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("input.txt");
String content = CharStreams.toString(reader);
Writer writer = new FileWriter("output.txt");
CharStreams.copy(reader, writer);
}
}
3. Files: Guava provides a convenient way to process files.For example, you can use the method of reading files in the specified character set to use the method of reading files, or use `Files.write (byte [] bytes, FILE)` to use the specified character set.Write the file.
import com.google.common.io.Files;
public class FilesExample {
public static void main(String[] args) throws IOException {
List<String> lines = Files.readLines(new File("input.txt"), Charset.defaultCharset());
byte[] byteArray = "Hello, World!".getBytes();
Files.write(byteArray, new File("output.txt"));
}
}
4. Filter (InputSupplier/OUTPUTSUPPLIER): GUAVA provides a supplier for filtering input/output.For example, you can use the `Files.asinPutsupplier (File File) method to convert the file into an input supply, and use the` Bytestreams.copy (InputSupplier.getInput (), OutputStream) `Copy the contents of the input of the supply to the output stream.Essence
import com.google.common.io.InputSupplier;
import com.google.common.io.OutputSupplier;
public class SuppliersExample {
public static void main(String[] args) throws IOException {
InputSupplier<FileInputStream> inputSupplier = Files.newInputStreamSupplier(new File("input.txt"));
OutputStream outputStream = new FileOutputStream("output.txt");
ByteStreams.copy(inputSupplier.getInput(), outputStream);
}
}
5. Resources: Guava provides convenient methods to handle resources in the class and file systems.For example, you can use the `Resources.getResource (String ResourceName) method to obtain resources on the class path, or use the` Resources.tobytearray (URL URL) `to convert URL resources into byte array.
import com.google.common.io.Resources;
public class ResourcesExample {
public static void main(String[] args) throws IOException {
URL resourceUrl = Resources.getResource("data.txt");
byte[] byteArray = Resources.toByteArray(resourceUrl);
}
}
By using Guava's input/output framework, you can handle files, streams and other data sources more conveniently.These components provide a simpler and more efficient method to read, write and operate data.I hope this article can provide you with a guide to how to use the Guava input/output framework, and to better understand its usage through the example code.