forked from LuigiCortese/java-certification-ocp8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.java
81 lines (58 loc) · 1.61 KB
/
App.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
package net.devsedge.io.file;
import java.io.File;
import java.io.IOException;
/**
*
* @author Luigi Cortese
*
*/
public class App {
public static void main(String[] args) throws IOException {
/*
* Creating a File object, it may be bound to an actual file or directory if it exist, otherwise
* it's an abstract representation.
*
* No file is being written in the file system yet.
*/
File file=new File("src/main/java/net/devsedge/io/file/foo.txt");
/*
* Checking if file exist.
*/
System.out.println(file.exists());
/*
* Checking if file is actually a file...
*/
System.out.println(file.isFile());
/*
* ...or a directory
*/
System.out.println(file.isDirectory());
/*
* Writing file to disk if it doesn't exist yet.
* When writing a file, specified path must exist, intermediate directories
* in the path won't be created and an exception will be thrown
*/
if(!file.exists())
file.createNewFile();
/*
* Create a new File object representing a directory
*/
File dir = new File("src/main/java/net/devsedge/io/file/my.dir");
/*
* Writing directory to disk
*/
dir.mkdir();
/*
* Create a new File object representing a directory
*/
File dir2 = new File("src/main/java/net/devsedge/io/file/intermediate.dir/my.dir");
/*
* Next instruction won't do anything, because intermediate directory doesn't exist
*/
dir2.mkdir();
/*
* This method creates intermediate directories
*/
dir2.mkdirs();
}
}