-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataHandling.java
66 lines (54 loc) · 1.93 KB
/
DataHandling.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
package hog;
import java.io.*;
/**
* Created by farkaskid on 24/3/16.
*/
public class DataHandling {
// FINDING THE FILE
private String findFile(int fileno, String location){
String file = Integer.toString(fileno);
char[] filearray = file.toCharArray();
StringBuffer location1 = new StringBuffer(location + "/Data");
for (int i = 0 ; i < filearray.length - 1 ; i++) {
location1.append("/" + Character.toString(filearray[i])) ;
}
return location1.append("/File" + file + ".txt").toString() ;
}
// INSERTING THE FILE
private String insertFile(int fileno, String location){
String file = Integer.toString(fileno);
char[] filearray = file.toCharArray();
StringBuffer location1 = new StringBuffer(location + "/Data");
for (int i = 0 ; i < filearray.length - 1 ; i++) {
location1.append("/" + Character.toString(filearray[i])) ;
}
if(!new File(location1.toString()).exists()){
(new File(location1.toString())).mkdirs();
}
return location1.append("/File" + file + ".txt").toString() ;
}
// GET A NEW READER
protected BufferedReader getReader(int fileno, String location){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(findFile(fileno, location)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return br;
}
// GET A NEW WRITER
protected BufferedWriter getWriter(int fileno, String location){
BufferedWriter br = null;
try {
br = new BufferedWriter(new FileWriter(insertFile(fileno, location)));
} catch (IOException e) {
e.printStackTrace();
}
return br;
}
// GET A FILE DESCRIPTOR
protected File getFile(int fileno, String location){
return (new File(findFile(fileno, location)));
}
}