-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIOStream.java
More file actions
74 lines (62 loc) · 2.34 KB
/
Copy pathFileIOStream.java
File metadata and controls
74 lines (62 loc) · 2.34 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
package io_nio2.io;
import io_nio2.Constant;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class FileIOStream {
public static void main(String[] args) {
String stringVal = read(Constant.COMMON_INPUT_PATH);
System.out.println(stringVal);
write(Constant.COMMON_OUTPUT_PATH, stringVal.concat("\n \n \"Edited\""));
byte[] copy = copy("src/resource/file/Image.png");
paste("src/resource/file/Image_copy.png", copy);
byte[] textCopy = copy(Constant.COMMON_INPUT_PATH);
String read = read(textCopy);
System.out.println(read);
}
//read String File
public static String read(String filePath){
StringBuilder stringBuilder = new StringBuilder();
try (FileInputStream fis = new FileInputStream(filePath)) {
int data;
while ((data = fis.read()) != -1) {
stringBuilder.append((char) data);
}
} catch (IOException e) {
throw new RuntimeException("Error reading file " + filePath, e);
}
return stringBuilder.toString();
}
//Convert From Byte[] to String
public static String read(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8);
}
//Get Byte[] from file
public static byte[] copy(String filePath){
try (FileInputStream fis = new FileInputStream(filePath)) {
return fis.readAllBytes();
} catch (IOException e) {
throw new RuntimeException("Error copy file " + filePath, e);
}
}
//create a new File From Byte[] Content
public static void paste(String filePath, byte[] bytes){
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(bytes);
} catch (IOException e) {
throw new RuntimeException("Error paste file " + filePath, e);
}
}
//Write String File
public static void write(String filePath, String bodyFile){
try (FileOutputStream fos = new FileOutputStream(filePath)) {
byte[] bytes = bodyFile.getBytes();
fos.write(bytes);
} catch (IOException e) {
throw new RuntimeException("Error writing file " + filePath, e);
}
}
}