-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.java
More file actions
83 lines (75 loc) · 2.2 KB
/
LogFile.java
File metadata and controls
83 lines (75 loc) · 2.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
package com.gmail.gazllloyd.springcleanerlog;
import com.gmail.gazllloyd.springcleanerlog.gui.SCFrame;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
import java.util.logging.Logger;
/**
* Created by Gareth Lloyd on 14/05/2015.
*/
public class LogFile implements Observer {
public static LogFile logfile;
static Logger log = SpringCleanerLog.log;
public File file;
public HashMap<String,SCItem> items;
public ArrayList<String> order;
public String name;
public SCFrame frame;
public LogFile(String path) {
this(new File(path));
}
public LogFile(File f) {
log.info("created logfile");
logfile = this;
file = f;
setup();
}
private void setup() {
log.info("setting up stuff");
CSVReader r;
try {
r = new CSVReader(new FileReader(file));
String[] nextline;
SCItem item;
order = new ArrayList<String>();
items = new HashMap<String, SCItem>();
while ((nextline = r.readNext()) != null) {
item = SCItem.makeSCItem(nextline);
item.addObserver(this);
order.add(item.getName());
items.put(item.getName(),item);
}
} catch(Exception e) {
log.severe("Could not load file!");
e.printStackTrace();
return;
}
String fn = file.getName();
fn = fn.substring(0,fn.lastIndexOf('.'));
name = fn;
}
private List<String[]> toWritable() {
ArrayList<String[]> w = new ArrayList<String[]>();
for (String i : order) {
w.add(items.get(i).getWritable());
}
return w;
}
@Override
public void update(Observable o, Object arg) {
log.info("Writing CSV");
CSVWriter w;
try {
w = new CSVWriter(new FileWriter(file));
w.writeAll(toWritable());
w.close();
} catch(Exception e) {
log.severe("Could not write to file!");
e.printStackTrace();
return;
}
}
}