1+ package nio2 ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .BufferedWriter ;
5+ import java .io .FileInputStream ;
6+ import java .io .FileOutputStream ;
7+ import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .io .OutputStream ;
10+ import java .nio .charset .Charset ;
11+ import java .nio .file .Files ;
12+ import java .nio .file .Path ;
13+ import java .nio .file .Paths ;
14+ import java .util .ArrayList ;
15+ import java .util .List ;
16+
17+ /**
18+ *
19+ * @author chengfeili
20+ * Jun 20, 2017 9:57:52 PM
21+ *
22+ * Path Object VS Actual File A Path object is not a file but a
23+ * representation of a location with the file system. Most operations
24+ * available in the Path and Paths classes can be accomplished
25+ * regardless of whether the underlying file that the path object
26+ * references actually exists.
27+ *
28+ * Java is fond of singular names for container classes and plural names
29+ * for factory and helper classes. I
30+ *
31+ * the Files class operates on Path instances, not File instances. Keep
32+ * in mind that File belongs to the legacy java.io API, while Files
33+ * belongs to the NIO.2 API.
34+ */
35+ public class InteractingWithPathsAndFiles {
36+
37+ public void testFile () {
38+ Files .exists (Paths .get ("/home/zoo" ));
39+ Files .exists (Paths .get ("/home/zoo/dog.jpg" ));
40+
41+ /**
42+ * The isSameFile() method first checks if the Path objects are equal in
43+ * terms of equal(), and if so, it automatically returns true without
44+ * checking to see if either files exists. If the Path object equals()
45+ * comparison returns false, then it locates each file to which the path
46+ * refers in the file system and determines if they are the same,
47+ * throwing a checked IOException if either file does not exist.
48+ */
49+ try {
50+ // cobra is a symbolic link to the snake
51+ System .out .println (Files .isSameFile (Paths .get ("/user/home/cobra" ), Paths .get ("/user/home/snake" ))); // true
52+ System .out .println (Files .isSameFile (Paths .get ("/user/tree/../monkey" ), Paths .get ("/user/monkey" ))); // true
53+ System .out .println (Files .isSameFile (Paths .get ("/leaves/./giraffe.exe" ), Paths .get ("/leaves/giraffe.exe" ))); // false
54+ System .out .println (Files .isSameFile (Paths .get ("/flamingo/tail.data" ), Paths .get ("/cardinal/tail.data" )));
55+ } catch (IOException e ) {
56+ // Handle file I/O exception...
57+ }
58+
59+ /**
60+ *
61+ * The example creates a new directory, field, in the directory /bison,
62+ * assuming /bison exists; or else an exception is thrown. Contrast this
63+ * with the second example that creates the directory green along with
64+ * any of the following parent directories if they do not already exist,
65+ * such as /bison, /bison/field, or /bison/pasture.
66+ */
67+ try {
68+ Files .createDirectory (Paths .get ("/bison/field" ));
69+ Files .createDirectories (Paths .get ("/bison/field/pasture/green" ));
70+ } catch (IOException e ) {
71+ // Handle file I/O exception...
72+ }
73+
74+ /**
75+ * Duplicating File Contents with copy()
76+ *
77+ * The copy() method throws the checked IOException, such as when the
78+ * file or directory does not exist or cannot be read. Directory copies
79+ * are shallow rather than deep, meaning that files and sub directories
80+ * within the directory are not copied. T
81+ */
82+ try {
83+ Files .copy (Paths .get ("/panda" ), Paths .get ("/panda-save" ));
84+
85+ Files .copy (Paths .get ("/panda/bamboo.txt" ), Paths .get ("/panda-save/bamboo.txt" ));
86+ } catch (IOException e ) {
87+ // Handle file I/O exception...
88+ }
89+
90+ try (InputStream is = new FileInputStream ("source-data.txt" );
91+ OutputStream out = new FileOutputStream ("output-data.txt" )) {
92+ // Copy stream data to file
93+ Files .copy (is , Paths .get ("c:\\ mammals\\ wolf.txt" ));
94+ // Copy file data to stream
95+ Files .copy (Paths .get ("c:\\ fish\\ clown.xsl" ), out );
96+ } catch (IOException e ) {
97+ // Handle file I/O exception... }
98+ }
99+ // Changing a File Location with move()
100+ try {
101+ Files .move (Paths .get ("c:\\ zoo" ), Paths .get ("c:\\ zoo-new" ));
102+ Files .move (Paths .get ("c:\\ user\\ addresses.txt" ), Paths .get ("c:\\ zoo-new\\ addresses.txt" ));
103+ } catch (IOException e ) {
104+ // Handle file I/O exception...
105+ }
106+
107+ /**
108+ * Removing a File with delete() and deleteIfExists()
109+ *
110+ * The first example deletes the features.txt file in the vulture
111+ * directory, and it throws a NoSuchFileException if the file or
112+ * directory does not exist. The second example deletes the pigeon
113+ * directory assuming it is empty. If the pigeon directory does not
114+ * exist, then the second line will not throw an exception.
115+ */
116+ try {
117+ Files .delete (Paths .get ("/vulture/feathers.txt" ));
118+ Files .deleteIfExists (Paths .get ("/pigeon" ));
119+ } catch (IOException e ) {
120+ // Handle file I/O exception...
121+ }
122+ /**
123+ *
124+ * Reading and Writing File Data with newBufferedReader() and
125+ * newBufferedWriter()
126+ */
127+
128+ Path path = Paths .get ("/animals/gopher.txt" );
129+ try (BufferedReader reader = Files .newBufferedReader (path , Charset .forName ("US-ASCII" ))) {
130+ // Read from the stream
131+ String currentLine = null ;
132+ while ((currentLine = reader .readLine ()) != null )
133+ System .out .println (currentLine );
134+ } catch (IOException e ) {
135+ // Handle file I/O exception... }
136+ }
137+
138+ try (BufferedWriter writer = Files .newBufferedWriter (path , Charset .forName ("UTF-16" ))) {
139+ writer .write ("Hello World" );
140+ } catch (IOException e ) {
141+ // Handle file I/O exception... }
142+ }
143+
144+ /**
145+ * Reading Files with readAllLines()
146+ */
147+ try {
148+ final List <String > lines = Files .readAllLines (path );
149+ for (String line : lines ) {
150+ System .out .println (line );
151+ }
152+ } catch (IOException e ) {
153+ // Handle file I/O exception...
154+ }
155+ }
156+
157+ }
0 commit comments