Skip to content

Commit 8e980e3

Browse files
committed
Nio code snippets
1 parent e834f52 commit 8e980e3

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.cdac.nio;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.nio.file.StandardCopyOption;
10+
import java.nio.file.StandardOpenOption;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.Collections;
14+
import java.util.List;
15+
16+
public class NioBasicDemo {
17+
18+
public static void main(String[] args) throws IOException {
19+
Path path = Paths.get("D:/a.txt");
20+
Path path2 = Paths.get("D:\\c.txt");
21+
22+
//FILE EXISTANCE
23+
System.out.println(Files.exists(path));
24+
System.out.println(Files.notExists(path));
25+
System.out.println("File exist : "+Files.exists(path2));
26+
27+
//IS READABLE IS WITABLE
28+
System.out.println("Regular file : "+Files.isRegularFile(path));
29+
System.out.println("Is Readable : "+Files.isReadable(path));
30+
System.out.println("Is Writable : "+Files.isWritable(path));
31+
System.out.println("Is executable : "+Files.isExecutable(path2));
32+
33+
//IS BOTH FILE SAME OR NOT
34+
// System.out.println("Is same file : "+Files.isSameFile(path, path2));
35+
36+
//FILE CREATION
37+
// System.out.println("Create file : "+Files.createFile(path2)); throws excetpion
38+
// Files.createFile(path); // trhows Exceptions
39+
/*Files.createDirectories(path);
40+
Files.createTempDirectory(path.toString());
41+
Files.createFile(path);
42+
Files.createTempFile(path,"abc",".txt");*/
43+
44+
// DELETE
45+
// Files.delete(path2); //throws exception if not exist
46+
//System.out.println("Removing a file : "+Files.deleteIfExists(path2));
47+
48+
//FILE COPY
49+
// Files.copy(path, path2); //throws exception file should not be available
50+
// Files.copy(path, path2, StandardCopyOption.REPLACE_EXISTING);
51+
52+
//MOVING FILES
53+
// Files.move(path, path2); // throws exception
54+
// Files.move(path, path2, StandardCopyOption.REPLACE_EXISTING); file a replace b, file a deleted
55+
56+
57+
//READ FILES
58+
byte[] buffer = Files.readAllBytes(path);
59+
System.out.println("Read by bytes :: "+new String(buffer));
60+
List<String> lines = Files.readAllLines(path);
61+
System.out.println(lines);
62+
/* BufferedReader br = Files.newBufferedReader(path2);
63+
br.readLine();
64+
br.close();*/
65+
66+
//WRITE FILES
67+
String content = "Good night";
68+
Files.write(path2, content.getBytes());
69+
Iterable<String> listLines = Arrays.asList("Hello","Hi");
70+
Files.write(path2, listLines, StandardOpenOption.CREATE);
71+
BufferedWriter bw = Files.newBufferedWriter(path2,StandardOpenOption.APPEND);
72+
bw.write("Home Work");
73+
bw.close();
74+
}
75+
}

0 commit comments

Comments
 (0)