-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogTheFile.java
More file actions
40 lines (32 loc) · 1.42 KB
/
Copy pathLogTheFile.java
File metadata and controls
40 lines (32 loc) · 1.42 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
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
import java.io.File;
public class LogTheFile {
public static void main(String[] args) {
System.out.println(System.getProperty("user.home") + File.separator + "Desktop");
// add the text file you want to log in place of "Rohit.txt" but also change fileName in log.command file
File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "LogFile.txt");
boolean doesFileExist = doesFileExist(file); // checks if the file exists
if(doesFileExist)
{
System.out.println("File found");
addLogInsideTheFile(file);
}
}
private static boolean doesFileExist(File file) {
// file should exist, not be a directory, as well as should have write permission
return file.exists() && file.isFile() && file.canWrite();
}
private static void addLogInsideTheFile(File file) {
String todaysDate = "\n" + new Date().toString() + "\n";
try {
System.out.println(todaysDate);
FileOutputStream f = new FileOutputStream(file,true); // write file in append mode
f.write(todaysDate.getBytes()); // add bytes corresponding to today
} catch (IOException e) {
e.printStackTrace();
}
}
}