1
+ import java .io .BufferedReader ;
2
+ import java .io .IOException ;
3
+ import java .nio .file .FileSystems ;
4
+ import java .nio .file .Files ;
5
+ import java .nio .file .Path ;
6
+ import java .nio .file .Paths ;
7
+
8
+ public class Main {
9
+
10
+ public static void main (String [] args ) {
11
+ Path path = FileSystems .getDefault ().getPath ("WorkingDirectoryFile.txt" );
12
+ printFile (path );
13
+ // Path filePath = FileSystems.getDefault().getPath("files", "SubdirectoryFile.txt");
14
+ Path filePath = Paths .get ("." , "files" , "SubdirectoryFile.txt" );
15
+ printFile (filePath );
16
+ filePath = Paths .get ("/JAVA MasterClass/Input & Output" , "/008_FileSystem" , "OutThere.txt" );
17
+ // filePath = Paths.get("D:\\", "Examples", OutThere.txt");
18
+ // D:\\Examples\\OutThere.txt
19
+ printFile (filePath );
20
+
21
+ filePath = Paths .get ("." );
22
+ System .out .println (filePath .toAbsolutePath ());
23
+ // D:\Examples\.\subfolder\..\directory
24
+ // D:\Examples\directory
25
+ Path path2 = FileSystems .getDefault ().getPath ("." , "files" , ".." , "files" , "SubdirectoryFile.txt" );
26
+ System .out .println (path2 .normalize ().toAbsolutePath ());
27
+ printFile (path2 .normalize ());
28
+
29
+
30
+ }
31
+
32
+ private static void printFile (Path path ) {
33
+ try (BufferedReader fileReader = Files .newBufferedReader (path )) {
34
+ String line ;
35
+ while ((line = fileReader .readLine ()) != null ) {
36
+ System .out .println (line );
37
+ }
38
+ } catch (IOException e ) {
39
+ System .out .println (e .getMessage ());
40
+ e .printStackTrace ();
41
+ }
42
+ }
43
+ }
0 commit comments