Java Streams概述二

140 阅读5分钟

链流

在使用流时,它们通常被链接在一起。

让我们看看使用链接流的优点是什么:

  • 一个流实例利用另一个流实例。
  • 这将创建更高级别的功能。我们可以有一个流访问数据,然后有另一个流获取数据的结果并处理更复杂的功能。
  • 这简化了可重用性,因为您可以以允许每个流执行特定工作的方式来组织流。这样,他们不需要知道彼此的内部运作方式。

我们使用构造函数执行链接。我们构造该流的更高级别的实例,然后传递一个较低级别的流的实例。InputStreamReader 类是链式流的一个很好的例子。此类通过在上提供读者行为来利用链接 InputStream_。_它将二进制响应转换为字符行为。

让我们看看它是如何工作的。

`void doChain(InputStream in) throws IOException{`
`int length;`
`char[] buffer = new char[128];`
`try(InputStreamReader rdr = new InputStreamReader(in)) {`
`while((length = rdr.read(buffer)) >= 0) {`
`//do something`
`}`
`}`
`}`

我们不需要关心其 InputStream 工作原理。它是由文件支持还是由网络支持都没有关系。我们唯一知道的是它将给我们二进制数据,我们将其传递给我们InputStreamReader 并将其转换为二进制数据并可以将其作为字符数据使用。

注意,我们try-with-resources 也在这里使用。如果我们关闭InputStreamReader,它InputStream 也会自动关闭。您应该知道这是一个非常强大的概念。

文件和缓冲流

我们经常使用流来访问文件。

_java.io包中_有几个要使用的类,例如:

  • FileReader
  • FileWriter
  • FileInputStream
  • FileOutputStream

真正的事实是这些文件流现在已被弃用。尽管如此,它们仍被广泛用于代码中,因此值得一提。

缓冲流

引入了缓冲流来替换_java.io_包中的 FileStream 类。这些新的Streams放在_java.nio_包下。这是必要的,因为直接文件访问可能效率低下,并且缓冲的流可以通过以下方式显着提高效率:

  • 缓冲内存中的内容
  • 大批量执行读/写
  • 减少底层流交互

缓冲可用于所有四种流类型:

  • BufferReader
  • BufferWriter
  • BufferedInputStream
  • BufferedOutputStream

使用它们非常简单。

`try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))){`
`int value;`
`while((value = br.read()) >= 0) {`
`char charValue = (char)value;`
`//do something`
`}`
`}`

使用 BufferedStreams 好处:

  • 它为各种平台(如Windows或Unix)处理换行符
  • 为当前平台使用正确的值
  • 这个BufferedWriter有一种方法:newLine() _._它将创建一个具有适当字符的新行。
  • 这个BufferedReader有一种基于行的读取方法:readLine().

让我们看看它们是如何工作的。

BufferedWriter:

`void writeData(String[] data) throws IOException {`
`try(BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))){`
`int value;`
`for(String str : data) {`
`bw.write(str);`
`bw.newLine();`
`}`
`}`

BufferedReader

`void readData(String[] data) throws IOException {`
`try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))){`
`String inValue;`
`while((inValue = br.readLine()) != null) {`
`System.out.println(inValue);`
`}`
`}`

上面的代码将逐行写出文件的内容。

使用java.nio.file包访问文件

在Java 8中,不赞成使用_java.io.FileXXX_流。 有一个用于处理文件流的新程序包,称为java.nio.file程序包。

与java.io相比,该软件包有几个好处:

  • 更好的异常报告
  • 更大的可扩展性,它们在处理大文件时效果更好
  • 更多文件系统功能支持
  • 简化常见任务

Path和Paths类型

Path:

  • 用于查找文件系统项
  • 它可以是文件或目录

Paths:

  • 用于通过静态Path工厂方法获取Path对象
  • 它将基于字符串的分层路径或URI转换为Path。

示例:Path p = Paths.get(“\\documents\\foo.txt”)

文件类型

  • 与文件交互的静态方法
  • 创建,复制,删除等…
  • 打开文件流
`1、newBufferedReader` 
`2、newBufferedWriter` 
`3、newInputStream` 
`4、newOutputStream` 
  • 读取/写入文件内容
`1、readAllLines`
`2、write` 

Reading Lines With BufferedReader

`void readData(String[] data) throws IOException {`
`try(BufferedReader br = Files.newBufferedReader(Paths.get("data.txt")){`
`String inValue;`
`while((inValue = br.readLine()) != null) {`
`System.out.println(inValue);`
`}`
`}`
`}`

Read All lines

`void readAllLines(String[] data) throws IOException {`
`List<String> lines = Files.readAllLines(Paths.get("data.txt"));`
`for(String line:lines) {`
`System.out.println();`
`}`
`}`

文件系统

当我们使用Java程序中的文件时,这些文件包含在文件系统中。最常见的是,我们使用计算机的默认文件系统。

Java还支持专用文件系统,例如_Zip文件_系统。

路径实例绑定到文件系统,Path 该类仅适用于默认实例。因此,我们需要另一个解决方案。幸运的是,在_Java.nio_包中,我们有机会对这一问题进行了处理。

文件系统类型

FileSystem

  • 代表一个单独的文件系统
  • Path 实例工厂

FileSystems

  • 用于FileSystem 通过静态FileSystem 工厂方法获取 对象
  • 打开或创建文件系统 newFileSystem

访问文件系统

URI标识的文件系统

  • URI的细节在文件系统之间差异很大
  • zip文件系统使用“ jar:file ”方案_jar:文件:/documents/data.zip_

文件系统支持自定义属性

  • 每种文件系统类型不同
  • 示例:字符串编码,如果不存在则是否创建

创建一个Zip文件系统

`public static void main(String[] args) throws FileNotFoundException, IOException {`
`try (FileSystem zipFileSystem = openZip(Paths.get("data.zip"))){ //pass the Path where we would like to create our FileSystem`
`}catch (Exception e) {`
`System.out.println(e.getClass().getSimpleName() + " - " + e.getLocalizedMessage());;`
`}`
`}`
`private static FileSystem openZip(Path path) throws URISyntaxException, IOException {`
`Map<String, String> properties = new HashMap<>();`
`properties.put("create", "true"); //set the property to allow creating`
`URI zipUri = new URI("jar:file", path.toUri().getPath(), null); //make a new URI from the path`
`FileSystem zipFileSystem = FileSystems.newFileSystem(zipUri, properties); //create the filesystem`
`return zipFileSystem;`
`}`

在上面的代码之后,您应该在目录中看到_data.zip_文件。

Java Streams概述二

将文件复制到Zip文件系统

让我们通过文件复制操作来扩展上述示例。

在此示例中,我在项目库中创建了一个名为_file.txt_的文件。我们会将这个文件复制到我们的_data.zip_文件系统中。

Java Streams概述二


`try (FileSystem zipFileSystem = openZip(Paths.get("data.zip"))){`
`copyFileToZip(zipFileSystem); //Here we call the file copy`
`}catch (Exception e) {`
`System.out.println(e.getClass().getSimpleName() + " - " + e.getLocalizedMessage());;`
`}`
`}`
`private static FileSystem openZip(Path path) throws URISyntaxException, IOException {`
`Map<String, String> properties = new HashMap<>();`
`properties.put("create", "true");`
`URI zipUri = new URI("jar:file", path.toUri().getPath(), null);`
`FileSystem zipFileSystem = FileSystems.newFileSystem(zipUri, properties);`
`return zipFileSystem;`
`}`
`static void copyFileToZip(FileSystem zipFileSystem) throws IOException{`
`Path sourceFile = FileSystems.getDefault().getPath("file.txt"); //Read the file to copy`
`Path destFile = zipFileSystem.getPath("/fileCopied.txt"); //get the path of the new file`
`Files.copy(sourceFile, destFile);//Copy the file to our zip FileSystem`
`}`

运行代码后,您应该在zip文件中看到_fileCopied.txt_。它的上下文应该与我们的_file.txt中_的相同_。_

Java Streams概述二

概括

在本文中,研究了Java 8中的流。演示了流链接是如何工作的,以及如何通过新的_java.nio_包处理文件。还谈到了为什么应该使用更多最新的缓冲版本的文件流。