IO.java
2.6 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
package com.air.agent.imf;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
public final class IO {
public static final int BUFFER = 4 * 1024;
public static void copy(File in, OutputStream out) throws FileNotFoundException, IOException {
copy(IO.createInputStream(in), out);
}
public static void copy(File in, File out) throws FileNotFoundException, IOException {
IO.copy(IO.createInputStream(in), out);
}
public static void copy(InputStream in, File out) throws FileNotFoundException, IOException {
copy(in, new BufferedOutputStream(new FileOutputStream(out)));
}
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] bytes = new byte[BUFFER];
for (int length = in.read(bytes); length > 0; length = in.read(bytes)) {
out.write(bytes, 0, length);
}
in.close();
out.close();
}
public static InputStream createInputStream(File file) throws FileNotFoundException {
return new BufferedInputStream(new FileInputStream(file));
}
public static Reader createReader(File file) throws FileNotFoundException {
return new BufferedReader(new FileReader(file));
}
public static Writer createWriter(File file) throws IOException {
return new BufferedWriter(new FileWriter(file));
}
public static byte[] readAsBytes(File in) throws FileNotFoundException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(IO.createInputStream(in), out);
return out.toByteArray();
}
public static byte[] readAsBytes(InputStream in) throws FileNotFoundException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(in, out);
return out.toByteArray();
}
public static String readAsString(File in) throws UnsupportedEncodingException, FileNotFoundException, IOException {
return new String(IO.readAsBytes(in), "UTF8");
}
public static String readAsString(InputStream in) throws UnsupportedEncodingException, FileNotFoundException, IOException {
return new String(IO.readAsBytes(in), "UTF8");
}
public static void write(String content, File file) throws IOException {
Writer writer = IO.createWriter(file);
writer.write(content);
writer.close();
}
}