-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKWriter.java
More file actions
130 lines (113 loc) · 3.74 KB
/
Copy pathKWriter.java
File metadata and controls
130 lines (113 loc) · 3.74 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
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
package net.kloud.framework;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class KWriter {
final static Charset ENCODING = StandardCharsets.UTF_8;
private File dir;
public KWriter(File file, String sub) throws IOException {
File newDir = new File(file, sub);
if(!newDir.exists()) {
newDir.mkdirs();
}
//Redundancy Call
if(!newDir.isDirectory()) {
newDir.mkdirs();
}
this.dir = newDir;
System.out.println(newDir.toPath().toString());
System.out.println("Created KWriter Instance");
}
public KWriter(File file) throws IOException {
File newDir = file;
if(!newDir.exists()) {
newDir.mkdirs();
}
//Redundancy Call
if(!newDir.isDirectory()) {
newDir.mkdirs();
}
this.dir = newDir;
System.out.println(newDir.toPath().toString());
System.out.println("Created KWriter Instance");
}
public void setSubDir(String sub) {
File newDir = new File(dir, sub);
if(!newDir.exists()) {
newDir.mkdirs();
}
if(!newDir.isDirectory()) {
newDir.mkdirs();
}
System.out.println("Created new sub-directory: " + sub + " in " + dir);
this.dir = newDir;
}
public void writeFile(List<String> strings) throws IOException {
if(strings == null) return;
String title = "/" + strings.get(0) + ".klouddata";
Path dirPath = Paths.get(dir.getPath());
File newFile = new File(dirPath + title);
if(newFile.exists() == false) newFile.createNewFile();
List<String> aList = new ArrayList<String>();
for(String line : strings) aList.add(line);
write(aList, newFile);
System.out.println("Call to KWriter.writeFile()");
}
private static void write(List<String> aList, File newFile) throws IOException {
Path path = Paths.get(newFile.getPath());
Files.write(path, aList, ENCODING);
System.out.println("Call to KWriter.write()");
}
public void writeFiles(List<List<String>> objTower) throws IOException {
for(List<String> aList : objTower) writeFile(aList);
System.out.println("Call to KWriter.writeFiles()");
}
private List<String> read(File file) throws IOException {
List<String> aList = new ArrayList<String>();
aList = Files.readAllLines(file.toPath());
System.out.println("Call to KWriter.read()");
return aList;
}
public List<List<String>> readDir() throws IOException {
List<List<String>> objTower = new ArrayList<List<String>>();
if(dir.isDirectory()) {
File[] files = dir.listFiles();
if(files.length == 1) return null;
for(File file : files) objTower.add(read(file));
}
System.out.println("Call to KWriter.readDir()");
return objTower;
}
public List<String> getFileNames() {
List<String> aList = new ArrayList<String>();
if(dir.isDirectory()) {
File[] files = dir.listFiles();
if(files == null) return null;
for(File file : files) {
aList.add(file.getName());
}
}
System.out.println("Call to KWriter.getFileNames()");
return aList;
}
public List<String> readOneFile(String fileName) throws IOException {
File tempFile = new File(this.dir, fileName);
List<String> aList = Files.readAllLines(tempFile.toPath());
return aList;
}
public void writeOneFile(String fileName, List<String> aList) throws IOException {
File tempFile = new File(this.dir, fileName);
if(!tempFile.exists()) tempFile.createNewFile();
write(aList, tempFile);
}
public static void writeOneFile(File tempFile, List<String> aList) throws IOException {
if(!tempFile.exists()) tempFile.createNewFile();
write(aList, tempFile);
}
}