|
@@ -1,11 +1,9 @@
|
1
|
1
|
import java.io.*;
|
2
|
|
-import java.nio.file.Path;
|
3
|
|
-import java.nio.file.Paths;
|
4
|
|
-import java.util.ArrayList;
|
|
2
|
+
|
5
|
3
|
import java.util.HashMap;
|
6
|
|
-import java.util.stream.IntStream;
|
7
|
4
|
import java.util.stream.Stream;
|
8
|
5
|
|
|
6
|
+
|
9
|
7
|
public class AtbashCipher{
|
10
|
8
|
|
11
|
9
|
private static final String abc = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
|
|
@@ -24,7 +22,6 @@ public class AtbashCipher{
|
24
|
22
|
for (int i = 0; i < abcArr.length; i++) {
|
25
|
23
|
key.put(abcArr[i], cbaArr[i]);
|
26
|
24
|
}
|
27
|
|
-
|
28
|
25
|
}
|
29
|
26
|
|
30
|
27
|
public static String cipher(String s) {
|
|
@@ -35,15 +32,10 @@ public class AtbashCipher{
|
35
|
32
|
charArray[i] = key.get(charArray[i]);
|
36
|
33
|
}
|
37
|
34
|
}
|
38
|
|
-
|
39
|
35
|
return charArrayToString(charArray);
|
40
|
36
|
}
|
41
|
37
|
|
42
|
|
- public static char swap(char c) {
|
43
|
|
- return key.get(c);
|
44
|
|
- }
|
45
|
|
-
|
46
|
|
- public static String charArrayToString(char[] chars) {
|
|
38
|
+ public static String charArrayToString(char[] chars) {
|
47
|
39
|
StringBuilder sb = new StringBuilder();
|
48
|
40
|
for (char c: chars) {
|
49
|
41
|
sb.append(c);
|
|
@@ -51,26 +43,28 @@ public class AtbashCipher{
|
51
|
43
|
return sb.toString();
|
52
|
44
|
}
|
53
|
45
|
|
54
|
|
- public void cipherFile(String filePath) throws FileNotFoundException {
|
|
46
|
+ public void cipherFile(String filePathIn, String filePathOut) throws FileNotFoundException {
|
|
47
|
+ String inFile = fileToString(filePathIn);
|
55
|
48
|
|
56
|
|
- String ourFile = fileToString(filePath);
|
57
|
|
-
|
58
|
|
- ArrayList<Character> charStream = ourFile.chars()
|
59
|
|
- .mapToObj(c -> (char) c)
|
60
|
|
- .forEach(AtbashCipher::swap)
|
61
|
|
-
|
62
|
|
-
|
63
|
|
-
|
64
|
|
-// char[] ciphered = Stream.of(ourFile)
|
65
|
|
-// .forEach(AtbashCipher::swap);
|
|
49
|
+ PrintStream file = new PrintStream(new File(filePathOut));
|
|
50
|
+ PrintStream console = System.out;
|
66
|
51
|
|
|
52
|
+ System.setOut(file);
|
67
|
53
|
|
|
54
|
+ String swappedChars = Stream.of(inFile)
|
|
55
|
+ .map(AtbashCipher::cipher)
|
|
56
|
+ .reduce("", String::concat);
|
68
|
57
|
|
|
58
|
+ System.out.println(swappedChars);
|
|
59
|
+ System.setOut(console);
|
69
|
60
|
}
|
70
|
61
|
|
|
62
|
+
|
71
|
63
|
private String fileToString(String filePath) throws FileNotFoundException {
|
72
|
64
|
|
73
|
|
- FileReader fReader = new FileReader(filePath);
|
|
65
|
+ File file = new File(filePath);
|
|
66
|
+
|
|
67
|
+ FileReader fReader = new FileReader(file);
|
74
|
68
|
BufferedReader bufferedReader = new BufferedReader(fReader);
|
75
|
69
|
|
76
|
70
|
StringBuilder sb = new StringBuilder();
|