-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
58 lines (44 loc) · 2.02 KB
/
Main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.IOException;
public class Main {
// using string
// public static void main(String[] args) {
// String input = "vanshtrivediharajabcjab";
// Huffman ed = new Huffman(input);
// String encodedString = ed.encode(input);
// System.out.println(encodedString);
// String decodedString = ed.decode(encodedString);
// System.out.println(decodedString);
// if (input.equals(decodedString)) {
// System.out.println("true");
// }
// }
// using file
public static void main(String[] args) {
try {
String inputFilePath = "C://Users//Vansh//Desktop//myname.txt";
String encodedFilePath = "C://Users//Vansh//Desktop//myname.encoded.txt";
String decodedFilePath = "C://Users//Vansh//Desktop//myname.decoded.txt";
// Read input from file
String input = Huffman.readFile(inputFilePath);
// Initialize Huffman coding with input
Huffman huffman = new Huffman(input);
// Encode the input and write to encoded file
String encodedString = huffman.encode(input);
System.out.println(encodedString);
Huffman.writeFile(encodedFilePath, encodedString);
// Read encoded file
String encodedInput = Huffman.readFile(encodedFilePath);
// Decode the encoded string and write to decoded file
String decodedString = huffman.decode(encodedInput);
Huffman.writeFile(decodedFilePath, decodedString);
// Verify if the decoded string matches the original input
if (input.equals(decodedString)) {
System.out.println("The decoded string matches the original input Verification successful!");
} else {
System.out.println("The decoded string does not match the originalinput.Verification failed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}