5
5
6
6
import java .io .File ;
7
7
import java .io .IOException ;
8
- import java .nio .file .Files ;
9
- import java .nio .file .Path ;
10
- import java .nio .file .Paths ;
11
8
12
9
/**
13
10
* @author bhuwan
@@ -17,31 +14,38 @@ public class CreateNewFile {
17
14
public static void main (String [] args ) throws IOException {
18
15
String fileSeparator = System .getProperty ("file.separator" );
19
16
// absolute file name with path
20
- String absoluteFilePath = fileSeparator + "home" + fileSeparator + "bhuwan" + fileSeparator + "filedemo"
21
- + fileSeparator + "file.txt" ;
17
+ String absoluteFilePath = fileSeparator + "tmp" + fileSeparator + "file.txt" ;
22
18
File file = new File (absoluteFilePath );
23
19
if (file .createNewFile ()) {
24
20
System .out .println (absoluteFilePath + " File Created" );
25
- }
26
- else
21
+ } else {
27
22
System .out .println ("File " + absoluteFilePath + " already exists" );
23
+ }
28
24
29
25
// file name only
30
26
file = new File ("file.txt" );
31
27
if (file .createNewFile ()) {
32
28
System .out .println ("file.txt File Created in Project root directory" );
33
- }
34
- else
29
+ } else {
35
30
System .out .println ("File file.txt already exists in project root directory" );
31
+ }
36
32
37
33
// relative path
38
- String relativePath = "tmp" + fileSeparator + "file.txt" ;
34
+ String relativePath = fileSeparator + "tmp" + fileSeparator + "file.txt" ;
39
35
file = new File (relativePath );
40
36
if (file .createNewFile ()) {
41
37
System .out .println (relativePath + " File Created in Project root tmp directory" );
42
- }
43
- else
38
+ } else {
44
39
System .out .println ("File " + relativePath + " already exists in project root tmp directory" );
40
+ }
41
+
42
+ // create directory
43
+ file = new File (fileSeparator + "tmp" , "test_directory" );
44
+ if (file .mkdir ()) {
45
+ System .out .println ("Directory named " + file + " created successfully." );
46
+ } else {
47
+ System .out .println ("Directory already exist: " + file );
48
+ }
45
49
}
46
50
47
51
}
0 commit comments