-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-tool.java
141 lines (106 loc) · 3.61 KB
/
csv-tool.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//Execute CRUD operations on CSV file
//Create, Read, Update, Delete entries
import java.util.HashMap;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.FileReader;
public class CSVTool
{
// data structure of choice for storing csv data
private static HashMap<String, String> csv_contents;
// input file name
final static String file_name = "input.csv";
public static void main(String[] args) {
//initialize hash map
csv_contents = new HashMap<String, String>();
InitFile();
//read file contents and update hash map
ReadFile();
System.out.println("Original file contents");
for(String i : csv_contents.keySet())
System.out.println(i + "," + csv_contents.get(i));
System.out.println();
System.out.println("Adding animal...");
AddNewAnimal("Parrot", "Dascal");
System.out.println("Deleting animal...");
DeleteAnimal("Ant");
System.out.println("Updating animal name...");
UpdateAnimalName("Tiger", "Bernard");
System.out.println("Reading animal name...");
ReadAnimalName("Spider");
System.out.println();
System.out.println("Modified contents are:");
for(String i : csv_contents.keySet())
System.out.println(i + "," + csv_contents.get(i));
System.out.println();
System.out.println("Updating file...");
UpdateFile();
System.out.println("File updated.");
}
public static void InitFile() {
File f = new File(file_name);
if(f.exists()) {
System.out.println("File already exists");
return;
}
//create file if none exists
try{
f.createNewFile();
} catch(IOException e){
e.printStackTrace();
}
}
public static void ReadFile() {
File f = new File(file_name);
BufferedReader br;
try{
br = new BufferedReader(new FileReader(file_name));
String line = br.readLine();
while(line != null){
// split line into animal type and name
String[] pair = line.split(",");
csv_contents.put(pair[0], pair[1]);
//next line
line = br.readLine();
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void UpdateFile() {
FileWriter fw;
try{
fw = new FileWriter(file_name);
StringBuilder sb = new StringBuilder();
for(String i : csv_contents.keySet()) {
//append animal type
sb.append(i);
//append separator
sb.append(",");
//append animal name
sb.append(csv_contents.get(i));
sb.append("\n");
}
fw.write(sb.toString());
fw.close();
} catch(Exception e){
e.printStackTrace();
}
}
public static void AddNewAnimal(String animal_type, String animal_name) {
csv_contents.put(animal_type, animal_name);
}
public static void UpdateAnimalName(String animal_type, String new_name) {
csv_contents.replace(animal_type, new_name);
}
public static void ReadAnimalName(String animal_type) {
String animal_name = csv_contents.get(animal_type);
System.out.println(animal_type + " name is: " + animal_name);
}
public static void DeleteAnimal(String animal_type) {
csv_contents.remove(animal_type);
}
}