-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay15.java
More file actions
111 lines (94 loc) · 3.2 KB
/
Day15.java
File metadata and controls
111 lines (94 loc) · 3.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 15 : File Handling
// Day 15 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Day15
{
// Basic User-file Interaction.
// * Let's first open a file, append this text inside
// "
// Hello, I am a test.
// Append a text at the end of the file.
// "
// * Close the file writer
// * Print the content of the file
// * Close the file writer
// * Append this text inside your file.
// "
// Added!
// "
// * When this is done,
// * Open the same file again, read the content,and print it.
private static final String PATH = "text_data/";
public static void main(String[] args)
{
File directory = new File(PATH);
// create directory if it does not exist
if( !(directory.exists()))
{
directory.mkdir();
}
File file = new File(PATH + "Day15_data.txt");
// creating the file if it does not exist
if(!file.exists())
{
try
{
if(file.createNewFile())
{
System.out.println("File successfully created : "+file.getName());
}
}catch(IOException e)
{
e.printStackTrace();
}
}
else // the file does not exist
{
System.out.println(
String.format("File %s already exists.", file.getName())
);
}
// writing and reading processes
try
{
// Write inside the file
FileWriter writer = new FileWriter(file.getAbsolutePath());
System.out.println("\n----------- WRITING ------------\n");
writer.write("Hello, I am a text.\nAppend this text inside your file.!\n");
writer.close();
Scanner sc = new Scanner(file);
System.out.println("\n----------- READING ------------\n");
// read the content of the file
while (sc.hasNextLine())
System.out.println(sc.nextLine());
sc.close();
System.out.println("\n----------- WRITING ------------\n");
// Append a text at the end of the file
writer = new FileWriter(file.getAbsolutePath(), true);
writer.write("Added!\n");
writer.close();
System.out.println("\n----------- READING ------------\n");
sc = new Scanner(file);
// read the content of the file
while (sc.hasNextLine())
System.out.println(sc.nextLine());
sc.close();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("\nEnd of Program\n");
}
}