-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyFile.java
162 lines (155 loc) · 5.09 KB
/
MyFile.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
package yourpackage;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
*
* @author Emil
* @param <T>
*/
public class MyFile<T>{
private String fileName;
private File f;
public MyFile(){
fileName = "MyFile.dat";
f = new File(fileName);
}
public MyFile(String fileName){
this.fileName = fileName;
f = new File(fileName);
}
@Override
public String toString(){
return fileName;
}
public long getTotalSpace(){
return f.getTotalSpace();
}
public T upload() {
try(ObjectInputStream IN = new ObjectInputStream(new FileInputStream(fileName))){
return (T) IN.readObject();
} catch (IOException | ClassNotFoundException | NullPointerException ex) {
return null;
}
}
public T[] upload(T[] e){
ArrayList<T> a = new ArrayList();
T t = null;
int i=0;
try(ObjectInputStream IN = new ObjectInputStream(new FileInputStream(fileName))){
while(true){
a.add((T) IN.readObject());
i++;
}
}
catch(EOFException ex){
t = a.get(0);
return a.toArray((T[])Array.newInstance(t.getClass(), i));
}
catch (IOException | ClassNotFoundException | NullPointerException ex) {
return null;
}
}
public T[] upload(int sizeArray){
ArrayList<T> a = new ArrayList();
try(ObjectInputStream IN = new ObjectInputStream(new FileInputStream(fileName))){
int i=0;
while(i<sizeArray){
a.add((T)IN.readObject());
i++;
}
T t = a.get(0);
return a.toArray((T[])Array.newInstance(t.getClass(), sizeArray));
}
catch (IOException | ClassNotFoundException | NullPointerException ex) {
return null;
}
}
public boolean download(T e, boolean append){
if(!f.exists() || !append){
try (ObjectOutputStream OUT = new ObjectOutputStream(new FileOutputStream(fileName))) {
OUT.writeObject(e);
return true;
} catch (IOException ex) {
return false;
}
}
else {
try (AppendingObjectOutputStream OUT = new AppendingObjectOutputStream(new FileOutputStream(fileName, append))) {
OUT.writeObject(e);
return true;
} catch (IOException ex) {
return false;
}
}
}
public boolean download(T[] e, boolean append){
if(!f.exists() || !append){
try (ObjectOutputStream OUT = new ObjectOutputStream(new FileOutputStream(fileName))) {
int i=0;
while(i<e.length){
OUT.writeObject(e[i]);
i++;
}
return true;
} catch (IOException ex) {
return false;
}
}
else {
try (AppendingObjectOutputStream OUT = new AppendingObjectOutputStream(new FileOutputStream(fileName, append))) {
int i=0;
while(i<e.length){
OUT.writeObject(e[i]);
i++;
}
return true;
} catch (IOException ex) {
return false;
}
}
}
public boolean download(T[] e, int size, boolean append){
if(!f.exists() || !append){
try (ObjectOutputStream OUT = new ObjectOutputStream(new FileOutputStream(fileName, append))) {
int i=0;
while(i<size && i<e.length){
OUT.writeObject(e[i]);
i++;
}
return true;
} catch (IOException | NullPointerException ex) {
return false;
}
}
else{
try (AppendingObjectOutputStream OUT = new AppendingObjectOutputStream(new FileOutputStream(fileName, append))) {
int i=0;
while(i<size && i<e.length){
OUT.writeObject(e[i]);
i++;
}
return true;
} catch (IOException ex) {
return false;
}
}
}
private class AppendingObjectOutputStream extends ObjectOutputStream{
protected AppendingObjectOutputStream(OutputStream out) throws IOException{
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
// do not write a header, but reset:
reset();
}
}
}