通过使用流,说明想要完成什么任务,而不是说明如何去实现它,将操作的调度留给具体实现去解决;
实例:假如我们想要计算某个属性的平均值,那么我们就可以指定数据源和属性,然后,流库就可以对计算进行优化;
从 java.nio.file 提供的功能不难看出已经可以替换 java.io.file 所提供的功能;
1:对文件系统本身的操作, 例如文件的复制, 移除, 删除, 创建功能, 创建软连接。
2:对文件系统的属性的进行操作, 例如查看或修改 文件属性、操作权限、所属用户或用户组、最后修改时间, 查看文件是否隐藏、文件的长度。
3:对文件系统进行遍历。
4:使用 nio 的方式查看和改变文件内容。
5:对文件或文件夹的创建, 删除, 修改事件进行监控。
1:复制文件
copy(Path source,Path target,CopyOption... options) throws IOException
2:创建目录
createDirectories(Path dir,FileAttribute<?>... attrs) throws IOException
3:创建文件, path 代表文件路径
createFile(Path path,FileAttribute<?>... attrs) throws IOException
4:创建连接, link 代表目标连接, existing 代表一个存在的文件
createLink(Path link,Path existing)throws IOException
5:删除文件
delete(Path path); deleteIfExists(Path path)
6:获取文件的 BufferReader,BufferWriter
newBufferedReader(Path path, Charset cs), newBufferedWriter(Path path, Charset cs, OpenOption... options)
7:获取文件的 InputStream,OutputStream
newInputStream(Path path, OpenOption... options),newOutputStream(Path path, OpenOption... options)
8:以字节和字符串形式读取文件
readAllBytes(Path path),readAllLines(Path path, Charset cs)
需求:对文档中的长单词进行计数
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Arrays;
- import java.util.List;
- /**
- * Created by Lenovo on 2017/12/14.
- * 对文件中的长单词进行计数
- */
- public class Demo01 {
- private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";
- public static void main(String[] args) throws Exception {
- //使用集合的方法实现
- String contens = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
- String[] ws = contens.split("\\PL+");
- //将数组转化为List集合
- List < String > words = Arrays.asList(ws);
- int count = 0;
- for (String word: words) {
- if (word.length() > 6) {
- count++;
- }
- }
- System.out.println(count);
- }
- }
java.util.Collection<E>:
default Stream<E> stream() ----- 产生当前集合中所有元素的顺序流
default Stream<E> parallelStream() ----- 产生当前集合中所有元素的并行流
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Arrays;
- import java.util.List;
- /**
- * Created by Lenovo on 2017/12/14.
- * 使用流对文档中的长单词进行计数
- *
- */
- public class Demo02 {
- private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";
- public static void main(String[] args) throws Exception {
- String contents = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
- String[] ws = contents.split("\\PL+");
- //将数组转化为集合
- List<String> words = Arrays.asList(ws);
- //使用流
- //Stream filter(Predicate predicate) 产生一个流,其中包含当前流中满足P的所有元素
- //long count() 产生当前流中元素的数量,这个是一个终止操作
- long count = words.stream()
- .filter(w -> w.length() > 6)
- .count();
- System.out.println("顺序流输出:"+count);
- long count02 = words.parallelStream()
- .filter(w -> w.length()>6)
- .count();
- System.out.println("并行流输出:"+count02);
- }
- }
流的主要思想是:做什么而非怎么做;
以上实例:需要统计文档中的长度为 6 的单词
1:流并不存储其元素;
2:流的操作不会修改其数据源
3:流的操作是尽可能惰性执行的
1:创建一个流
2:指定将初始流转化为其他流的中间操作,可能包含多个步骤(filter,产生新的流);
3:应用终止操作,从而产生结果
来源: http://www.bubuko.com/infodetail-2429197.html