-
Notifications
You must be signed in to change notification settings - Fork 668
/
Files.java
4102 lines (3806 loc) · 213 KB
/
Files.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileTreeWalker.Event;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileStoreAttributeView;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.spi.FileSystemProvider;
import java.nio.file.spi.FileTypeDetector;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiPredicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jdk.internal.misc.JavaLangAccess;
import jdk.internal.misc.SharedSecrets;
import sun.nio.ch.FileChannelImpl;
import sun.nio.fs.AbstractFileSystemProvider;
/**
* This class consists exclusively of static methods that operate on files,
* directories, or other types of files.
*
* <p> In most cases, the methods defined here will delegate to the associated
* file system provider to perform the file operations.
*
* @since 1.7
*/
// File操作工具类
public final class Files {
private static final Set<OpenOption> DEFAULT_CREATE_OPTIONS = Set.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
// buffer size used for reading and writing
private static final int BUFFER_SIZE = 8192;
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
private Files() {
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 目录流遍历(非递归) ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Opens a directory, returning a {@link DirectoryStream} to iterate over
* all entries in the directory. The elements returned by the directory
* stream's {@link DirectoryStream#iterator iterator} are of type {@code
* Path}, each one representing an entry in the directory. The {@code Path}
* objects are obtained as if by {@link Path#resolve(Path) resolving} the
* name of the directory entry against {@code dir}.
*
* <p> When not using the try-with-resources construct, then directory
* stream's {@code close} method should be invoked after iteration is
* completed so as to free any resources held for the open directory.
*
* <p> When an implementation supports operations on entries in the
* directory that execute in a race-free manner then the returned directory
* stream is a {@link SecureDirectoryStream}.
*
* @param dir the path to the directory
*
* @return a new and open {@code DirectoryStream} object
*
* @throws NotDirectoryException if the file could not otherwise be opened because it is not
* a directory <i>(optional specific exception)</i>
* @throws IOException if an I/O error occurs
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the directory.
*/
// 返回指定实体的目录流,用来搜寻目录内的直接子项(不会过滤任何子项)
public static DirectoryStream<Path> newDirectoryStream(Path dir) throws IOException {
return provider(dir).newDirectoryStream(dir, AcceptAllFilter.FILTER);
}
/**
* Opens a directory, returning a {@link DirectoryStream} to iterate over
* the entries in the directory. The elements returned by the directory
* stream's {@link DirectoryStream#iterator iterator} are of type {@code
* Path}, each one representing an entry in the directory. The {@code Path}
* objects are obtained as if by {@link Path#resolve(Path) resolving} the
* name of the directory entry against {@code dir}. The entries returned by
* the iterator are filtered by the given {@link DirectoryStream.Filter
* filter}.
*
* <p> When not using the try-with-resources construct, then directory
* stream's {@code close} method should be invoked after iteration is
* completed so as to free any resources held for the open directory.
*
* <p> Where the filter terminates due to an uncaught error or runtime
* exception then it is propagated to the {@link Iterator#hasNext()
* hasNext} or {@link Iterator#next() next} method. Where an {@code
* IOException} is thrown, it results in the {@code hasNext} or {@code
* next} method throwing a {@link DirectoryIteratorException} with the
* {@code IOException} as the cause.
*
* <p> When an implementation supports operations on entries in the
* directory that execute in a race-free manner then the returned directory
* stream is a {@link SecureDirectoryStream}.
*
* <p> <b>Usage Example:</b>
* Suppose we want to iterate over the files in a directory that are
* larger than 8K.
* <pre>
* DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
* public boolean accept(Path file) throws IOException {
* return (Files.size(file) > 8192L);
* }
* };
* Path dir = ...
* try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
* :
* }
* </pre>
*
* @param dir the path to the directory
* @param filter the directory stream filter
*
* @return a new and open {@code DirectoryStream} object
*
* @throws NotDirectoryException if the file could not otherwise be opened because it is not
* a directory <i>(optional specific exception)</i>
* @throws IOException if an I/O error occurs
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the directory.
*/
// 返回指定实体的目录流,用来搜寻目录内的直接子项(需要自定义过滤器)
public static DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
return provider(dir).newDirectoryStream(dir, filter);
}
/**
* Opens a directory, returning a {@link DirectoryStream} to iterate over
* the entries in the directory. The elements returned by the directory
* stream's {@link DirectoryStream#iterator iterator} are of type {@code
* Path}, each one representing an entry in the directory. The {@code Path}
* objects are obtained as if by {@link Path#resolve(Path) resolving} the
* name of the directory entry against {@code dir}. The entries returned by
* the iterator are filtered by matching the {@code String} representation
* of their file names against the given <em>globbing</em> pattern.
*
* <p> For example, suppose we want to iterate over the files ending with
* ".java" in a directory:
* <pre>
* Path dir = ...
* try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.java")) {
* :
* }
* </pre>
*
* <p> The globbing pattern is specified by the {@link
* FileSystem#getPathMatcher getPathMatcher} method.
*
* <p> When not using the try-with-resources construct, then directory
* stream's {@code close} method should be invoked after iteration is
* completed so as to free any resources held for the open directory.
*
* <p> When an implementation supports operations on entries in the
* directory that execute in a race-free manner then the returned directory
* stream is a {@link SecureDirectoryStream}.
*
* @param dir the path to the directory
* @param glob the glob pattern
*
* @return a new and open {@code DirectoryStream} object
*
* @throws java.util.regex.PatternSyntaxException if the pattern is invalid
* @throws NotDirectoryException if the file could not otherwise be opened because it is not
* a directory <i>(optional specific exception)</i>
* @throws IOException if an I/O error occurs
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the directory.
*/
// 返回指定实体的目录流,用来搜寻目录内的直接子项(需要根据指定的"glob"正则来构造目录流过滤器)
public static DirectoryStream<Path> newDirectoryStream(Path dir, String glob) throws IOException {
/* avoid creating a matcher if all entries are required */
// "*"即匹配一切
if(glob.equals("*")) {
// 返回指定实体的目录流,用来搜寻目录内的直接子项(不会过滤任何子项)
return newDirectoryStream(dir);
}
/* create a matcher and return a filter that uses it */
// 返回当前路径所属的文件系统
FileSystem fs = dir.getFileSystem();
// 获取一个由"glob"正则构造的路径匹配器(参见Globs类)
final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
// 构造一个过滤器,匹配过滤条件的目录会被访问
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<>() {
@Override
public boolean accept(Path entry) {
return matcher.matches(entry.getFileName());
}
};
// 返回指定实体的目录流,用来搜寻目录内的直接子项
return fs.provider().newDirectoryStream(dir, filter);
}
/*▲ 目录流遍历(非递归) ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 文件树遍历(递归) ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Walks a file tree.
*
* <p> This method works as if invoking it were equivalent to evaluating the
* expression:
* <blockquote><pre>
* walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
* </pre></blockquote>
* In other words, it does not follow symbolic links, and visits all levels
* of the file tree.
*
* @param start the starting file
* @param visitor the file visitor to invoke for each file
*
* @return the starting file
*
* @throws SecurityException If the security manager denies access to the starting file.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String) checkRead} method is invoked
* to check read access to the directory.
* @throws IOException if an I/O error is thrown by a visitor method
*/
/*
* 递归遍历指定的文件树
*
* start : 遍历起点
* visitor : 文件树遍历回调,用来处理遍历过程中发生的事件
*/
public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException {
return walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor);
}
/**
* Walks a file tree.
*
* <p> This method walks a file tree rooted at a given starting file. The
* file tree traversal is <em>depth-first</em> with the given {@link
* FileVisitor} invoked for each file encountered. File tree traversal
* completes when all accessible files in the tree have been visited, or a
* visit method returns a result of {@link FileVisitResult#TERMINATE
* TERMINATE}. Where a visit method terminates due an {@code IOException},
* an uncaught error, or runtime exception, then the traversal is terminated
* and the error or exception is propagated to the caller of this method.
*
* <p> For each file encountered this method attempts to read its {@link
* java.nio.file.attribute.BasicFileAttributes}. If the file is not a
* directory then the {@link FileVisitor#visitFile visitFile} method is
* invoked with the file attributes. If the file attributes cannot be read,
* due to an I/O exception, then the {@link FileVisitor#visitFileFailed
* visitFileFailed} method is invoked with the I/O exception.
*
* <p> Where the file is a directory, and the directory could not be opened,
* then the {@code visitFileFailed} method is invoked with the I/O exception,
* after which, the file tree walk continues, by default, at the next
* <em>sibling</em> of the directory.
*
* <p> Where the directory is opened successfully, then the entries in the
* directory, and their <em>descendants</em> are visited. When all entries
* have been visited, or an I/O error occurs during iteration of the
* directory, then the directory is closed and the visitor's {@link
* FileVisitor#postVisitDirectory postVisitDirectory} method is invoked.
* The file tree walk then continues, by default, at the next <em>sibling</em>
* of the directory.
*
* <p> By default, symbolic links are not automatically followed by this
* method. If the {@code options} parameter contains the {@link
* FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then symbolic links are
* followed. When following links, and the attributes of the target cannot
* be read, then this method attempts to get the {@code BasicFileAttributes}
* of the link. If they can be read then the {@code visitFile} method is
* invoked with the attributes of the link (otherwise the {@code visitFileFailed}
* method is invoked as specified above).
*
* <p> If the {@code options} parameter contains the {@link
* FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then this method keeps
* track of directories visited so that cycles can be detected. A cycle
* arises when there is an entry in a directory that is an ancestor of the
* directory. Cycle detection is done by recording the {@link
* java.nio.file.attribute.BasicFileAttributes#fileKey file-key} of directories,
* or if file keys are not available, by invoking the {@link #isSameFile
* isSameFile} method to test if a directory is the same file as an
* ancestor. When a cycle is detected it is treated as an I/O error, and the
* {@link FileVisitor#visitFileFailed visitFileFailed} method is invoked with
* an instance of {@link FileSystemLoopException}.
*
* <p> The {@code maxDepth} parameter is the maximum number of levels of
* directories to visit. A value of {@code 0} means that only the starting
* file is visited, unless denied by the security manager. A value of
* {@link Integer#MAX_VALUE MAX_VALUE} may be used to indicate that all
* levels should be visited. The {@code visitFile} method is invoked for all
* files, including directories, encountered at {@code maxDepth}, unless the
* basic file attributes cannot be read, in which case the {@code
* visitFileFailed} method is invoked.
*
* <p> If a visitor returns a result of {@code null} then {@code
* NullPointerException} is thrown.
*
* <p> When a security manager is installed and it denies access to a file
* (or directory), then it is ignored and the visitor is not invoked for
* that file (or directory).
*
* @param start the starting file
* @param options options to configure the traversal
* @param maxDepth the maximum number of directory levels to visit
* @param visitor the file visitor to invoke for each file
*
* @return the starting file
*
* @throws IllegalArgumentException if the {@code maxDepth} parameter is negative
* @throws SecurityException If the security manager denies access to the starting file.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String) checkRead} method is invoked
* to check read access to the directory.
* @throws IOException if an I/O error is thrown by a visitor method
*/
/*
* 递归遍历指定的文件树
*
* start : 遍历起点
* options : 文件树遍历选项,指示对于符号链接,是否将其链接到目标文件;如果显式设置了LinkOption.NOFOLLOW_LINKS,表示不链接
* maxDepth: 遍历深度
* visitor : 文件树遍历回调,用来处理遍历过程中发生的事件
*/
public static Path walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor) throws IOException {
/*
* Create a FileTreeWalker to walk the file tree, invoking the visitor for each event.
*/
// 创建一个文件树访问器
try(FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
// 访问给定的实体(文件或目录),返回一个遍历事件以指示下一步应该如何决策
FileTreeWalker.Event ev = walker.walk(start);
do {
FileVisitResult result;
switch(ev.type()) {
// 遇到了不可遍历的实体:遇到异常,递归层次达到上限,遇到文件而不是目录
case ENTRY:
IOException ioe = ev.ioeException();
// 如果递归层次达到上限,或遇到文件而不是目录
if(ioe == null) {
assert ev.attributes() != null;
result = visitor.visitFile(ev.file(), ev.attributes());
// 如果遍历过程出现了异常
} else {
result = visitor.visitFileFailed(ev.file(), ioe);
}
break;
// 遇到了目录
case START_DIRECTORY:
result = visitor.preVisitDirectory(ev.file(), ev.attributes());
/* if SKIP_SIBLINGS and SKIP_SUBTREE is returned then there shouldn't be any more events for the current directory */
// 如果不需要遍历当前目录的子项,或不需要遍历当前目录的其他兄弟项
if(result == FileVisitResult.SKIP_SUBTREE || result == FileVisitResult.SKIP_SIBLINGS) {
// 将位于目录栈栈顶的目录结点出栈,并关闭其对应的目录流
walker.pop();
}
break;
// 结束了对指定目录的遍历
case END_DIRECTORY:
result = visitor.postVisitDirectory(ev.file(), ev.ioeException());
/* SKIP_SIBLINGS is a no-op for postVisitDirectory */
// SKIP_SIBLINGS信号不适合postVisitDirectory(),如果存在,会被更改为CONTINUE操作
if(result == FileVisitResult.SKIP_SIBLINGS) {
result = FileVisitResult.CONTINUE;
}
break;
default:
throw new AssertionError("Should not get here");
}
// 如果返回结果不是CONTINUE,需要进一步处理
if(Objects.requireNonNull(result) != FileVisitResult.CONTINUE) {
// 如果返回结果是TERMINATE,则跳出循环,结束遍历
if(result == FileVisitResult.TERMINATE) {
break;
// 如果返回结果是SKIP_SIBLINGS,说明需要跳过其他兄弟项
} else if(result == FileVisitResult.SKIP_SIBLINGS) {
walker.skipRemainingSiblings();
}
}
// 返回对下一个兄弟项或子项的遍历事件。如果子项都被遍历完了,则返回top目录遍历结束的事件
ev = walker.next();
// 如果目录栈不为空(说明仍然需要遍历)
} while(ev != null);
}
return start;
}
/*▲ 文件树遍历(递归) ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 流式操作 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Return a lazily populated {@code Stream}, the elements of
* which are the entries in the directory. The listing is not recursive.
*
* <p> The elements of the stream are {@link Path} objects that are
* obtained as if by {@link Path#resolve(Path) resolving} the name of the
* directory entry against {@code dir}. Some file systems maintain special
* links to the directory itself and the directory's parent directory.
* Entries representing these links are not included.
*
* <p> The stream is <i>weakly consistent</i>. It is thread safe but does
* not freeze the directory while iterating, so it may (or may not)
* reflect updates to the directory that occur after returning from this
* method.
*
* <p> The returned stream contains a reference to an open directory.
* The directory is closed by closing the stream.
*
* <p> Operating on a closed stream behaves as if the end of stream
* has been reached. Due to read-ahead, one or more elements may be
* returned after the stream has been closed.
*
* <p> If an {@link IOException} is thrown when accessing the directory
* after this method has returned, it is wrapped in an {@link
* UncheckedIOException} which will be thrown from the method that caused
* the access to take place.
*
* @param dir The path to the directory
*
* @return The {@code Stream} describing the content of the
* directory
*
* @throws NotDirectoryException if the file could not otherwise be opened because it is not
* a directory <i>(optional specific exception)</i>
* @throws IOException if an I/O error occurs when opening the directory
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the directory.
* @apiNote This method must be used within a try-with-resources statement or similar
* control structure to ensure that the stream's open directory is closed
* promptly after the stream's operations have completed.
* @see #newDirectoryStream(Path)
* @since 1.8
*/
// 返回指定目录的流(非递归遍历)
public static Stream<Path> list(Path dir) throws IOException {
// 返回指定实体的目录流,用来搜寻目录内的子文件/目录(不会过滤任何子项)
DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
try {
// 返回目录流迭代器,用来遍历目录内的子项
final Iterator<Path> delegate = ds.iterator();
/* Re-wrap DirectoryIteratorException to UncheckedIOException */
// 重新包装delegate
Iterator<Path> iterator = new Iterator<>() {
@Override
public boolean hasNext() {
try {
return delegate.hasNext();
} catch(DirectoryIteratorException e) {
throw new UncheckedIOException(e.getCause());
}
}
@Override
public Path next() {
try {
return delegate.next();
} catch(DirectoryIteratorException e) {
throw new UncheckedIOException(e.getCause());
}
}
};
// 构造"适配Iterator"的Spliterator(引用类型版本)
Spliterator<Path> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
// 构造处于源头(head)阶段的流(引用类型版本)
Stream<Path> stream = StreamSupport.stream(spliterator, false);
// 获取一个Runnable,执行对目录流的关闭操作
Runnable runnable = asUncheckedRunnable(ds);
// 为stream注册关闭回调:当stream关闭时,顺便将目录流一起关闭
return stream.onClose(runnable);
} catch(Error | RuntimeException e) {
try {
ds.close();
} catch(IOException ex) {
try {
e.addSuppressed(ex);
} catch(Throwable ignore) {
}
}
throw e;
}
}
/**
* Return a {@code Stream} that is lazily populated with {@code
* Path} by searching for files in a file tree rooted at a given starting
* file.
*
* <p> This method walks the file tree in exactly the manner specified by
* the {@link #walk walk} method. For each file encountered, the given
* {@link BiPredicate} is invoked with its {@link Path} and {@link
* BasicFileAttributes}. The {@code Path} object is obtained as if by
* {@link Path#resolve(Path) resolving} the relative path against {@code
* start} and is only included in the returned {@link Stream} if
* the {@code BiPredicate} returns true. Compare to calling {@link
* java.util.stream.Stream#filter filter} on the {@code Stream}
* returned by {@code walk} method, this method may be more efficient by
* avoiding redundant retrieval of the {@code BasicFileAttributes}.
*
* <p> The returned stream contains references to one or more open directories.
* The directories are closed by closing the stream.
*
* <p> If an {@link IOException} is thrown when accessing the directory
* after returned from this method, it is wrapped in an {@link
* UncheckedIOException} which will be thrown from the method that caused
* the access to take place.
*
* @param start the starting file
* @param maxDepth the maximum number of directory levels to search
* @param matcher the function used to decide whether a file should be included
* in the returned stream
* @param options options to configure the traversal
*
* @return the {@link Stream} of {@link Path}
*
* @throws IllegalArgumentException if the {@code maxDepth} parameter is negative
* @throws SecurityException If the security manager denies access to the starting file.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String) checkRead} method is invoked
* to check read access to the directory.
* @throws IOException if an I/O error is thrown when accessing the starting file.
* @apiNote This method must be used within a try-with-resources statement or similar
* control structure to ensure that the stream's open directories are closed
* promptly after the stream's operations have completed.
* @see #walk(Path, int, FileVisitOption...)
* @since 1.8
*/
/*
* 返回指定目录的流(可以递归遍历)
*
* maxDepth:最大递归层次
* matcher :对遍历到的文件/目录进行过滤,只保存满足matcher条件的目录
* options :对于符号链接,是否将其链接到目标文件;如果显式设置了LinkOption.NOFOLLOW_LINKS,表示不链接
*/
public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) throws IOException {
// 获取文件树迭代器
FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
try {
// 构造"适配Iterator"的Spliterator(引用类型版本)
Spliterator<FileTreeWalker.Event> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
// 构造处于源头(head)阶段的流(引用类型版本)
Stream<Event> stream = StreamSupport.stream(spliterator, false);
return stream.onClose(iterator::close) // 为stream注册关闭回调:当stream关闭时,顺便将迭代器一起关闭
.filter(entry -> matcher.test(entry.file(), entry.attributes())) // 只保存满足matcher条件的目录
.map(event -> event.file()); // 获取到文件路径
} catch(Error | RuntimeException e) {
iterator.close();
throw e;
}
}
/**
* Return a {@code Stream} that is lazily populated with {@code
* Path} by walking the file tree rooted at a given starting file. The
* file tree is traversed <em>depth-first</em>, the elements in the stream
* are {@link Path} objects that are obtained as if by {@link
* Path#resolve(Path) resolving} the relative path against {@code start}.
*
* <p> This method works as if invoking it were equivalent to evaluating the
* expression:
* <blockquote><pre>
* walk(start, Integer.MAX_VALUE, options)
* </pre></blockquote>
* In other words, it visits all levels of the file tree.
*
* <p> The returned stream contains references to one or more open directories.
* The directories are closed by closing the stream.
*
* @param start the starting file
* @param options options to configure the traversal
*
* @return the {@link Stream} of {@link Path}
*
* @throws SecurityException If the security manager denies access to the starting file.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String) checkRead} method is invoked
* to check read access to the directory.
* @throws IOException if an I/O error is thrown when accessing the starting file.
* @apiNote This method must be used within a try-with-resources statement or similar
* control structure to ensure that the stream's open directories are closed
* promptly after the stream's operations have completed.
* @see #walk(Path, int, FileVisitOption...)
* @since 1.8
*/
// 返回指定目录的流(可以递归遍历)
public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException {
return walk(start, Integer.MAX_VALUE, options);
}
/**
* Return a {@code Stream} that is lazily populated with {@code
* Path} by walking the file tree rooted at a given starting file. The
* file tree is traversed <em>depth-first</em>, the elements in the stream
* are {@link Path} objects that are obtained as if by {@link
* Path#resolve(Path) resolving} the relative path against {@code start}.
*
* <p> The {@code stream} walks the file tree as elements are consumed.
* The {@code Stream} returned is guaranteed to have at least one
* element, the starting file itself. For each file visited, the stream
* attempts to read its {@link BasicFileAttributes}. If the file is a
* directory and can be opened successfully, entries in the directory, and
* their <em>descendants</em> will follow the directory in the stream as
* they are encountered. When all entries have been visited, then the
* directory is closed. The file tree walk then continues at the next
* <em>sibling</em> of the directory.
*
* <p> The stream is <i>weakly consistent</i>. It does not freeze the
* file tree while iterating, so it may (or may not) reflect updates to
* the file tree that occur after returned from this method.
*
* <p> By default, symbolic links are not automatically followed by this
* method. If the {@code options} parameter contains the {@link
* FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then symbolic links are
* followed. When following links, and the attributes of the target cannot
* be read, then this method attempts to get the {@code BasicFileAttributes}
* of the link.
*
* <p> If the {@code options} parameter contains the {@link
* FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then the stream keeps
* track of directories visited so that cycles can be detected. A cycle
* arises when there is an entry in a directory that is an ancestor of the
* directory. Cycle detection is done by recording the {@link
* java.nio.file.attribute.BasicFileAttributes#fileKey file-key} of directories,
* or if file keys are not available, by invoking the {@link #isSameFile
* isSameFile} method to test if a directory is the same file as an
* ancestor. When a cycle is detected it is treated as an I/O error with
* an instance of {@link FileSystemLoopException}.
*
* <p> The {@code maxDepth} parameter is the maximum number of levels of
* directories to visit. A value of {@code 0} means that only the starting
* file is visited, unless denied by the security manager. A value of
* {@link Integer#MAX_VALUE MAX_VALUE} may be used to indicate that all
* levels should be visited.
*
* <p> When a security manager is installed and it denies access to a file
* (or directory), then it is ignored and not included in the stream.
*
* <p> The returned stream contains references to one or more open directories.
* The directories are closed by closing the stream.
*
* <p> If an {@link IOException} is thrown when accessing the directory
* after this method has returned, it is wrapped in an {@link
* UncheckedIOException} which will be thrown from the method that caused
* the access to take place.
*
* @param start the starting file
* @param maxDepth the maximum number of directory levels to visit
* @param options options to configure the traversal
*
* @return the {@link Stream} of {@link Path}
*
* @throws IllegalArgumentException if the {@code maxDepth} parameter is negative
* @throws SecurityException If the security manager denies access to the starting file.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String) checkRead} method is invoked
* to check read access to the directory.
* @throws IOException if an I/O error is thrown when accessing the starting file.
* @apiNote This method must be used within a try-with-resources statement or similar
* control structure to ensure that the stream's open directories are closed
* promptly after the stream's operations have completed.
* @since 1.8
*/
// 返回指定目录的流(可以递归遍历,maxDepth可指定递归深度)
public static Stream<Path> walk(Path start, int maxDepth, FileVisitOption... options) throws IOException {
// 获取文件树迭代器
FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
try {
// 构造"适配Iterator"的Spliterator(引用类型版本)
Spliterator<FileTreeWalker.Event> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
// 构造处于源头(head)阶段的流(引用类型版本)
Stream<Event> stream = StreamSupport.stream(spliterator, false);
return stream.onClose(iterator::close) // 为stream注册关闭回调:当stream关闭时,顺便将迭代器一起关闭
.map(Event::file); // 获取到文件路径
} catch(Error | RuntimeException e) {
iterator.close();
throw e;
}
}
/**
* Read all lines from a file as a {@code Stream}. Bytes from the file are
* decoded into characters using the {@link StandardCharsets#UTF_8 UTF-8}
* {@link Charset charset}.
*
* <p> The returned stream contains a reference to an open file. The file
* is closed by closing the stream.
*
* <p> The file contents should not be modified during the execution of the
* terminal stream operation. Otherwise, the result of the terminal stream
* operation is undefined.
*
* <p> This method works as if invoking it were equivalent to evaluating the
* expression:
* <pre>{@code
* Files.lines(path, StandardCharsets.UTF_8)
* }</pre>
*
* @param path the path to the file
*
* @return the lines from the file as a {@code Stream}
*
* @throws IOException if an I/O error occurs opening the file
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the file.
* @apiNote This method must be used within a try-with-resources statement or similar
* control structure to ensure that the stream's open file is closed promptly
* after the stream's operations have completed.
* @since 1.8
*/
// 返回基于指定文件的行的流,默认接受UTF_8类型的编码
public static Stream<String> lines(Path path) throws IOException {
return lines(path, StandardCharsets.UTF_8);
}
/**
* Read all lines from a file as a {@code Stream}. Unlike {@link
* #readAllLines(Path, Charset) readAllLines}, this method does not read
* all lines into a {@code List}, but instead populates lazily as the stream
* is consumed.
*
* <p> Bytes from the file are decoded into characters using the specified
* charset and the same line terminators as specified by {@code
* readAllLines} are supported.
*
* <p> The returned stream contains a reference to an open file. The file
* is closed by closing the stream.
*
* <p> The file contents should not be modified during the execution of the
* terminal stream operation. Otherwise, the result of the terminal stream
* operation is undefined.
*
* <p> After this method returns, then any subsequent I/O exception that
* occurs while reading from the file or when a malformed or unmappable byte
* sequence is read, is wrapped in an {@link UncheckedIOException} that will
* be thrown from the
* {@link java.util.stream.Stream} method that caused the read to take
* place. In case an {@code IOException} is thrown when closing the file,
* it is also wrapped as an {@code UncheckedIOException}.
*
* @param path the path to the file
* @param cs the charset to use for decoding
*
* @return the lines from the file as a {@code Stream}
*
* @throws IOException if an I/O error occurs opening the file
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the file.
* @apiNote This method must be used within a try-with-resources statement or similar
* control structure to ensure that the stream's open file is closed promptly
* after the stream's operations have completed.
* @implNote This implementation supports good parallel stream performance for the
* standard charsets {@link StandardCharsets#UTF_8 UTF-8},
* {@link StandardCharsets#US_ASCII US-ASCII} and
* {@link StandardCharsets#ISO_8859_1 ISO-8859-1}. Such
* <em>line-optimal</em> charsets have the property that the encoded bytes
* of a line feed ('\n') or a carriage return ('\r') are efficiently
* identifiable from other encoded characters when randomly accessing the
* bytes of the file.
*
* <p> For non-<em>line-optimal</em> charsets the stream source's
* spliterator has poor splitting properties, similar to that of a
* spliterator associated with an iterator or that associated with a stream
* returned from {@link BufferedReader#lines()}. Poor splitting properties
* can result in poor parallel stream performance.
*
* <p> For <em>line-optimal</em> charsets the stream source's spliterator
* has good splitting properties, assuming the file contains a regular
* sequence of lines. Good splitting properties can result in good parallel
* stream performance. The spliterator for a <em>line-optimal</em> charset
* takes advantage of the charset properties (a line feed or a carriage
* return being efficient identifiable) such that when splitting it can
* approximately divide the number of covered lines in half.
* @see #readAllLines(Path, Charset)
* @see #newBufferedReader(Path, Charset)
* @see java.io.BufferedReader#lines()
* @since 1.8
*/
// 返回基于指定文件的行的流,cs为指定文件的字符编码,接受UTF_8/ISO_8859/US_ASCII类型
public static Stream<String> lines(Path path, Charset cs) throws IOException {
/*
* Use the good splitting spliterator if:
* 1) the path is associated with the default file system;
* 2) the character set is supported; and
* 3) the file size is such that all bytes can be indexed by int values (this limitation is imposed by ByteBuffer)
*/
if(path.getFileSystem() == FileSystems.getDefault() && FileChannelLinesSpliterator.SUPPORTED_CHARSET_NAMES.contains(cs.name())) {
// 创建一个File Channel,默认为只读
FileChannel fc = FileChannel.open(path, StandardOpenOption.READ);
// 返回指定文件的流,该文件以FileChannel的形式给出。如果过大,返回null
Stream<String> stream = createFileChannelLinesStream(fc, cs);
if(stream != null) {
return stream;
}
fc.close();
}
BufferedReader reader = Files.newBufferedReader(path, cs);
// 返回指定文件的流,该文件以BufferedReader的形式给出
return createBufferedReaderLinesStream(reader);
}
/*▲ 流式操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 字节流 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Opens a file, returning an input stream to read from the file. The stream
* will not be buffered, and is not required to support the {@link
* InputStream#mark mark} or {@link InputStream#reset reset} methods. The
* stream will be safe for access by multiple concurrent threads. Reading
* commences at the beginning of the file. Whether the returned stream is
* <i>asynchronously closeable</i> and/or <i>interruptible</i> is highly
* file system provider specific and therefore not specified.
*
* <p> The {@code options} parameter determines how the file is opened.
* If no options are present then it is equivalent to opening the file with
* the {@link StandardOpenOption#READ READ} option. In addition to the {@code
* READ} option, an implementation may also support additional implementation
* specific options.
*
* @param path the path to the file to open
* @param options options specifying how the file is opened
*
* @return a new input stream
*
* @throws IllegalArgumentException if an invalid combination of options is specified
* @throws UnsupportedOperationException if an unsupported option is specified
* @throws IOException if an I/O error occurs
* @throws SecurityException In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to the file.
*/
// 返回path处文件的输入流,以便从中读取数据
public static InputStream newInputStream(Path path, OpenOption... options) throws IOException {
return provider(path).newInputStream(path, options);
}
/**
* Opens or creates a file, returning an output stream that may be used to
* write bytes to the file. The resulting stream will not be buffered. The
* stream will be safe for access by multiple concurrent threads. Whether
* the returned stream is <i>asynchronously closeable</i> and/or
* <i>interruptible</i> is highly file system provider specific and
* therefore not specified.
*
* <p> This method opens or creates a file in exactly the manner specified
* by the {@link #newByteChannel(Path, Set, FileAttribute[]) newByteChannel}
* method with the exception that the {@link StandardOpenOption#READ READ}
* option may not be present in the array of options. If no options are
* present then this method works as if the {@link StandardOpenOption#CREATE
* CREATE}, {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING},
* and {@link StandardOpenOption#WRITE WRITE} options are present. In other
* words, it opens the file for writing, creating the file if it doesn't
* exist, or initially truncating an existing {@link #isRegularFile
* regular-file} to a size of {@code 0} if it exists.
*
* <p> <b>Usage Examples:</b>
* <pre>
* Path path = ...
*
* // truncate and overwrite an existing file, or create the file if
* // it doesn't initially exist