WikiStart: ExamIO.txt

File ExamIO.txt, 4.0 KB (added by 152071, 5 years ago)
Line 
1import java.io.*;
2import java.util.ArrayList;
3import java.util.List;
4
5/**
6 * @author Riste Stojanov
7 */
8public class ExamIo {
9
10 public static void main(String[] args) throws IOException {
11 File f = new File(".");
12 System.out.println(f.getAbsolutePath());
13 ExamIo instance = new ExamIo();
14 instance.copyLargeTxtFiles("unexisting", "data/to", 20);
15
16 instance.copyLargeTxtFiles("data/CsvToOrderedBinaryDatabase.csv", "data/to", 20);
17
18
19 instance.copyLargeTxtFiles("data/from", "data/to", 20);
20
21 List<byte[]> data = new ArrayList<>();
22 data.add("123".getBytes());
23 data.add("234".getBytes());
24 data.add("456".getBytes());
25 data.add("789".getBytes());
26 data.add("901".getBytes());
27
28 instance.serializeData("data/dest.bin", data);
29 byte[] el = instance.deserializeDataAtPosition("data/dest.bin", 3, 3);
30 System.out.println(new String(el));
31
32 el = instance.deserializeDataAtPosition("data/dest.bin", 1, 3);
33 System.out.println(new String(el));
34
35
36 }
37
38
39 public void serializeData(String destination, List<byte[]> data) throws IOException {
40 File f = new File(destination);
41 System.out.println(f.getAbsolutePath());
42 f.createNewFile();
43 try (FileOutputStream fos = new FileOutputStream(f, false)) {
44 for (byte[] el : data) {
45 for (byte b : el) {
46 fos.write((int) b);
47 }
48 }
49 }
50 }
51
52 public byte[] deserializeDataAtPosition(String source,
53 long position,
54 long elementLength) throws IOException {
55 try (
56 RandomAccessFile raf = new RandomAccessFile(source, "r")
57 ) {
58 raf.seek(position * elementLength);
59 byte[] el = new byte[(int) elementLength];
60
61 int elPos = 0;
62 while (elPos < elementLength) {
63 el[elPos] = (byte) raf.read();
64 if (el[elPos] == -1) {
65 throw new IllegalStateException("Not enough bytes for the element");
66 }
67 elPos++;
68 }
69 return el;
70 }
71 }
72
73 public void copyLargeTxtFiles(String from,
74 String to,
75 long size) {
76
77 try {
78 File fromDir = validateFrom(from);
79
80 File toDir = createUnexistingTo(to);
81
82 moveLargeTxtFiles(fromDir, toDir, size);
83 } catch (Exception e) {
84 System.err.println(e.getMessage());
85 e.printStackTrace();
86 }
87
88 }
89
90 private void moveLargeTxtFiles(File fromDir, File toDir, long size) {
91 File[] largeTxtFiles = fromDir.listFiles(new FileFilter() {
92
93 @Override
94 public boolean accept(File childFile) {
95 if (childFile.isFile() &&
96 childFile.getName().endsWith(".txt") &&
97 childFile.length() > size) {
98 return true;
99 } else if (childFile.isDirectory()) {
100 moveLargeTxtFiles(childFile, toDir, size);
101 }
102 return false;
103 }
104 });
105
106 for (File f : largeTxtFiles) {
107 copyFileContent(f, toDir);
108 }
109
110 }
111
112 private void copyFileContent(File f, File toDir) {
113 try (
114 FileInputStream fis = new FileInputStream(f);
115 FileOutputStream fos = new FileOutputStream(new File(toDir, f.getName()))
116 ) {
117 int b;
118 while ((b = fis.read()) != -1) {
119 fos.write(b);
120 }
121 } catch (IOException e) {
122 e.printStackTrace();
123 }
124 }
125
126 private File createUnexistingTo(String to) {
127 File toDir = new File(to);
128 if (!toDir.exists()) {
129 toDir.mkdirs();
130 } else if (!toDir.isDirectory()) {
131 throw new IllegalStateException(to + " ne e direktorium");
132 }
133
134 return toDir;
135 }
136
137 private File validateFrom(String from) {
138
139 File fromDir = new File(from);
140 if (!fromDir.exists()) {
141
142 throw new IllegalArgumentException("Ne postoi: " + fromDir.getAbsolutePath());
143 }
144 if (!fromDir.isDirectory()) {
145 throw new IllegalArgumentException("Ne e direktorium");
146
147 }
148 return fromDir;
149 }
150}