-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReadWrite.java
206 lines (192 loc) · 5.72 KB
/
FileReadWrite.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Write a description of class FileReader here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.*;
import java.util.ArrayList;
public class FileReadWrite
{
BufferedReader br;
BufferedWriter bw;
String fileName;
File file;
File tempFile;
public FileReadWrite(String name, boolean append)
{
file = new File(name);
fileName = file.getAbsolutePath();
file = new File(fileName);
if(!append)
file.delete();
}
public ArrayList<String> read()
{
ArrayList<String> lines = new ArrayList<String>();
String line;
try{
FileReader fr = new FileReader(fileName);
br = new BufferedReader(fr);
while((line = br.readLine()) != null)
lines.add(line);
fr.close();
} catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
}
return lines;
}
public String findLine(String word)
{
String line;
try{
FileReader fr = new FileReader(fileName);
br = new BufferedReader(fr);
while((line = br.readLine()) != null)
{
String tmp = line.split(" ")[0];
if(tmp.equals(word))
{
return line;
}
}
fr.close();
} catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
}
return "";
}
public String findClosest(String word)
{
String line;
String largest = "";
int longest = 0;
try{
FileReader fr = new FileReader(fileName);
br = new BufferedReader(fr);
while((line = br.readLine()) != null)
{
if(! line.equals(""))
{
int num = getLike(word, line);
if(num > longest)
{
longest = num;
largest = line;
}
}
}
fr.close();
} catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
}
return largest;
}
public void write(String ... lines)
{
try{
if (!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(fileName, true);
bw = new BufferedWriter(fw);
for(String line : lines)
{
bw.write(line);
bw.newLine();
}
} catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException ex) {
System.out.println("Error");
ex.printStackTrace();
}
}
}
public void replace(String newLine, int choice, int moves, boolean good)
{
tempFile = new File(fileName + ".tmp");
try{
FileWriter fw = new FileWriter(tempFile);
bw = new BufferedWriter(fw);
FileReader fr = new FileReader(fileName);
br = new BufferedReader(fr);
String line = "";
while((line = br.readLine()) != null)
{
if(!line.equals(newLine))
{
bw.write(line);
bw.newLine();
} else {
String weights = line.split(" ")[1];
String toAdd = line.split(" ")[0] + " ";
String nodes[] = weights.split("[)]");
int changed = (int) Integer.parseInt(nodes[choice].split("[(]")[1]);
if(good)
{
changed += moves;
if(changed > 100)
changed = 100;
}
else
{
changed -= moves;
if(changed < 0)
changed = 0;
}
for(int i = 0; i < nodes.length; i++)
{
if(i == choice)
{
toAdd += choice + "(" + changed + ")";
} else {
toAdd += nodes[i] + ")";
}
}
bw.write(toAdd);
bw.newLine();
}
}
fr.close();
} catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException ex) {
System.out.println("Error");
ex.printStackTrace();
}
}
update();
}
public void update()
{
System.gc();
file.setWritable(true);
file.delete();
tempFile.renameTo(file);
}
public int getLike(String line1, String line2)
{
String lines1[] = line1.split("");
String lines2[] = line2.split("");
int count = 0;
for(int i = 0; i < lines1.length; i++)
{
if(lines1[i].equals(lines2[i]))
count++;
}
return count;
}
}