1@Test
2public void givenUsingJava7_whenWritingToFile_thenCorrect()
3 throws IOException {
4 String str = "Hello";
5
6 Path path = Paths.get(fileName);
7 byte[] strToBytes = str.getBytes();
8
9 Files.write(path, strToBytes);
10
11 String read = Files.readAllLines(path).get(0);
12 assertEquals(str, read);
13}
14
1package FileHandling;
2
3 // Import the File class
4import java.io.File;
5
6// Import the IOException class to handle errors
7import java.io.IOException;
8
9public class CreateFile {
10public static void main(String[] args) {
11try {
12// Creating an object of a file
13File myObj = new File("D:FileHandlingNewFilef1.txt");
14if (myObj.createNewFile()) {
15System.out.println("File created: " + myObj.getName());
16} else {
17System.out.println("File already exists.");
18}
19} catch (IOException e) {
20System.out.println("An error occurred.");
21e.printStackTrace();
22}
23}
24}