• 【Java技术专题】「Java8技术盲区」让我们来看看新一代IO流的开发指引(流升级功能体系)


    IO流

    在使用流时,让我们看看使用IO流是什么,一个流实例利用另一个流实例。

    IO流具有的传输性

    这将创建更高级别的功能,我们可以有一个流访问数据,然后有另一个流获取数据的结果并处理更复杂的功能。

    IO流之间的透明化

    这简化了可重用性,因为您可以以允许每个流执行特定工作的方式来组织流。这样,他们不需要知道彼此的内部运作方式。

    IO流之间的传递性
    • 使用构造函数执行链接,构造流的更高级别的实例,然后传递一个较低级别的流的实例。

    • InputStreamReader类是IO流的一个很好的例子,此类通过在上提供读取行为来利用接收 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
    	  }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    IO流的工作本质

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

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

    文件操作IO流

    经常使用流来访问文件。

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

    • FileReader
    • FileWriter
    • FileInputStream
    • FileOutputStream

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

    缓冲类型IO流

    引入了缓冲流来替换java.io包中的File操作IO流类,这些新的Stream放在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
         }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    Buffered IO流的好处:

    各种平台(如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();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    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);
         }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    Java8专属程序包

    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);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    文件系统
    默认文件系统

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

    Zip文件系统

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

    文件系统类型
    • FileSystem:代表一个单独的文件系统,Path实例工厂

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

    访问文件系统

    URI标识的文件系统:zip文件系统使用“ jar:file ”

    ###文件系统支持自定义属性

    每种文件系统类型不同

    示例:字符串编码,如果不存在则是否创建

    创建一个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
          //make a new URI from the path
          URI zipUri = new URI("jar:file", path.toUri().getPath(), null);
          FileSystem zipFileSystem = FileSystems.newFileSystem(zipUri, properties); 
          //create the filesystem
          return zipFileSystem;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

    将文件复制到Zip文件系统

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

    在项目库中创建了一个名为file.txt的文件。会将这个文件复制到我们的data.zip文件系统中。

    
       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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    联合概率和条件概率的区别和联系
    Python 获取class_name win32gui
    20220725树状数组入门反思
    1.验证码绕过
    java毕业设计校园实习管理系统mybatis+源码+调试部署+系统+数据库+lw
    几种ubuntu下deb打包技术:checkinstall打包成deb,sh打包成可执行文件,以及添加依赖库,到打包到deb
    猿创征文|Redis删除策略
    设计模式:解释器模式
    asp毕业设计——基于asp+access的文学网站设计与实现(毕业论文+程序源码)——文学网站
    redisson解决redis服务器的主从一致性问题
  • 原文地址:https://blog.csdn.net/l569590478/article/details/127935321