-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathTimeUpdater.java
64 lines (57 loc) · 1.74 KB
/
TimeUpdater.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by JohnTsai on 16/1/11.
*/
public class TimeUpdater {
private String fileName;
private String dateString;
private static final String formatStr = "yyyy-MM-dd kk:mm";
public TimeUpdater(String fileName){
this.fileName = fileName;
this.dateString = getDateString(new Date());
}
private String read(){
File file = new File(fileName);
Path path = file.toPath();
String str = null;
try {
str = new String(Files.readAllBytes(path));
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
private void write(String string){
byte data[] = string.getBytes();
try {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(){
String str = read();
System.out.println(dateString);
String updateStr= str.replaceFirst("[0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}", this.dateString);
write(updateStr);
}
public static String getDateString(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
return sdf.format(date);
}
public static void main(String[] args) {
TimeUpdater timeUpdater = new TimeUpdater("./README.MD");
timeUpdater.update();
}
}