建设网站需要了解什么,用什么来网站开发好,部队网站建设总结,百度快照优化排名推广上周#xff0c;我讨论了类Pattern新的#xff08;since 1.8#xff09;方法splitAsStream可以处理字符序列#xff0c;仅从流中读取该字符序列#xff0c;并且不进行模式匹配以创建所有可能的元素并返回它作为流。 这种行为是流的本质#xff0c;它是支持高性能应用程序… 上周我讨论了类Pattern新的since 1.8方法splitAsStream可以处理字符序列仅从流中读取该字符序列并且不进行模式匹配以创建所有可能的元素并返回它作为流。 这种行为是流的本质它是支持高性能应用程序的必经之路。 正如我在上周所承诺的那样在本文中我将展示splitAsStream的实际应用在该应用中处理流而不是将整个字符串拆分成数组并对其进行处理确实有意义。 您可能已经从文章标题中猜到了该应用程序正在将文件与一些标记分离。 只要文件长度不超过2GB就可以将其表示为CharSequence 。 限制来自以下事实 CharSequence的长度是int值在Java中为32位。 文件长度为long 为64位。 由于从文件读取比从已在内存中的字符串读取要慢得多因此使用流处理的惰性是有意义的。 我们需要的是一个由文件备份的字符序列实现。 如果可以的话我们可以编写如下程序 public static void main(String[] args) throws FileNotFoundException {Pattern p Pattern.compile([,\\.\\-;]);final CharSequence splitIt new FileAsCharSequence(new File(path_to_source\\SplitFileAsStream.java));p.splitAsStream(splitIt).forEach(System.out::println);} 此代码不读取文件的任何部分但不需要假定实现FileAsCharSequence不会读取文件贪婪的内容。 FileAsCharSequence类的实现可以是 package com.epam.training.regex;import java.io.*;public class FileAsCharSequence implements CharSequence {private final int length;private final StringBuilder buffer new StringBuilder();private final InputStream input;public FileAsCharSequence(File file) throws FileNotFoundException {if (file.length() (long) Integer.MAX_VALUE) {throw new IllegalArgumentException(File is too long to handle as character sequence);}this.length (int) file.length();this.input new FileInputStream(file);}Overridepublic int length() {return length;}Overridepublic char charAt(int index) {ensureFilled(index 1);return buffer.charAt(index);}Overridepublic CharSequence subSequence(int start, int end) {ensureFilled(end 1);return buffer.subSequence(start, end);}private void ensureFilled(int index) {if (buffer.length() index) {buffer.ensureCapacity(index);final byte[] bytes new byte[index - buffer.length()];try {int length input.read(bytes);if (length bytes.length) {throw new IllegalArgumentException(File ended unexpected);}} catch (IOException e) {throw new RuntimeException(e);}try {buffer.append(new String(bytes, utf-8));} catch (UnsupportedEncodingException ignored) {}}}
} 该实现仅从文件中读取那么多字节这是对charAt或subSequence的最后一个实际方法调用所需要的。 如果您有兴趣可以改进此代码以仅将真正需要的字节保留在内存中并删除已经返回到流中的字节。 要知道不需要什么字节上一篇文章提供了一个很好的提示那就是splitAsStream绝不会接触索引比最后一次调用subSequence的第一个 start 参数小的subSequence 。 但是如果您以一种丢弃字符的方式实现代码并且如果有人要访问已经抛出的字符而失败那么它将无法真正实现CharSequence接口尽管只要使用splitAsStream 它仍然可以很好地工作。只要实现不会改变并且开始需要一些已经传递的字符。 嗯我不确定但是如果我们使用一些复杂的正则表达式作为拆分模式也可能会发生这种情况。 编码愉快 翻译自: https://www.javacodegeeks.com/2017/11/split-file-stream.html