Skip to content

Commit 74fb3f1

Browse files
committed
Ch 17 ex: 20
1 parent 753daf3 commit 74fb3f1

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/ch_17/exercise17_20/Exercise17_20.java

+28-24
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
import javafx.scene.layout.VBox;
1616
import javafx.stage.Stage;
1717

18-
import java.io.IOException;
19-
import java.io.PrintWriter;
18+
import java.io.*;
2019
import java.nio.charset.StandardCharsets;
2120
import java.nio.file.Paths;
2221
import java.util.ArrayList;
@@ -97,54 +96,59 @@ public void start(Stage primaryStage) throws Exception {
9796
private void saveBytesToFile(String text) throws IOException {
9897
int counter = 0;
9998
ArrayList<String> splitBytes = new ArrayList<>();
100-
String temp = "";
99+
StringBuilder strByte = new StringBuilder();
101100
for (char ch : text.toCharArray()) {
102101
if (counter < 8) {
103-
temp += ch;
102+
strByte.append(ch);
104103
counter++;
105104
} else {
106-
splitBytes.add(temp);
105+
splitBytes.add(strByte.toString());
107106
counter = 0;
108-
temp = "";
107+
strByte = new StringBuilder();
109108
}
110109
}
111-
PrintWriter printWriter = new PrintWriter(filePath, StandardCharsets.UTF_8.name());
110+
FileOutputStream fOs = new FileOutputStream(filePath);
111+
StringBuilder stringBuilder = new StringBuilder();
112+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fOs);
112113
char ch = 0;
113114
for (String bits : splitBytes) {
114-
ch = getCharFromByte(bits);
115-
printWriter.write(ch);
115+
ch = (char) getCharFromByte(bits);
116+
stringBuilder.append(ch);
116117
}
117-
printWriter.flush();
118-
printWriter.close();
118+
119+
outputStreamWriter.write(stringBuilder.toString());
120+
outputStreamWriter.close();
119121

120122
}
121123

122124
private String readBytesFromFile() throws IOException {
123125
StringBuilder result = new StringBuilder();
124-
Scanner in = new Scanner(Paths.get(filePath), StandardCharsets.UTF_8.name());
125-
while (in.hasNextLine()) {
126-
String line = in.nextLine();
127-
for (char ch : line.toCharArray()) {
128-
result.append(getBits(ch));
129-
}
126+
FileInputStream fIs = new FileInputStream(filePath);
127+
InputStreamReader iSr = new InputStreamReader(fIs);
128+
Reader in = new BufferedReader(iSr);
129+
int ch;
130+
while ((ch = in.read()) != -1) {
131+
String valueAsByte = getBits(ch);
132+
result.append(valueAsByte);
130133
}
131134
in.close();
132135

133136
return result.toString();
137+
134138
}
135139

136140
public static String getBits(int value) {
137-
String bits = "";
138-
for (int i = 0; i < 8; i++) {
139-
bits += (value & 1);
140-
value >>= 1;
141+
StringBuilder bits = new StringBuilder();
142+
long i;
143+
for (i = 128; i > 0; i /= 2) {
144+
bits.append((value & i) != 0 ? "1" : "0");
141145
}
142-
return bits;
146+
return bits.toString();
143147
}
144148

145-
public static char getCharFromByte(String value) {
149+
public static int getCharFromByte(String value) {
146150
int numVal = Integer.parseInt(value, 2);
147-
return (char) numVal;
151+
return numVal;
148152
}
149153

150154

src/ch_17/exercise17_20/testFile.txt

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
Hello Text file,
2-
Hello test file,
3-
Hello Hello Hello World,
4-
What what WHAT World,
5-
That's not my hair, that's my bunny rabbit,
6-
HelloooooooooooooOOooOOoOoOooOoOooOo.
1+
Hʱcòš¶e@É+„·d@ݓ—ŒH

0 commit comments

Comments
 (0)