-
Notifications
You must be signed in to change notification settings - Fork 16
/
FileOutputHelper.java
35 lines (30 loc) · 1.16 KB
/
FileOutputHelper.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
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputHelper {
// Length of outputStr must be multiple of 8;
static void writeBinStrToFile(String outputStr, String outputFileName) {
int strLen = outputStr.length();
if (strLen % 8 != 0) {
System.err
.printf("Length of outputStr must a multiple of 8! Tried to write binary string: %s\n",
outputStr);
System.exit(1);
}
byte[] toWrite = new byte[strLen / 8];
for (int i = 0; i < outputStr.length() / 8; i++) {
toWrite[i] = (byte) Integer.parseInt(
outputStr.substring(i * 8, (i + 1) * 8), 2);
}
FileOutputStream output;
try {
output = new FileOutputStream(outputFileName, true);
output.write(toWrite);
output.close();
} catch (FileNotFoundException e) {
System.err.printf("Can't find file %s", outputFileName);
} catch (IOException e) {
System.err.println("Error with writing to output file");
}
}
}