forked from FBM2/Encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
45 lines (41 loc) · 1.39 KB
/
Copy pathEncoder.java
File metadata and controls
45 lines (41 loc) · 1.39 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
import java.util.*;
import java.io.*;
public class Encoder {
ArrayList<String> arr = new ArrayList<String>();
ArrayList<Character> file = new ArrayList<Character>();
//dont talk about it
public Encoder()
{
for (int i=0; i<95; i++)
{
arr.add(""+(char)(i+32)); //inputs values into table
}
}
public void encodeText(String fileName) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(fileName));
char character;
while (br.ready()){
character = (char)(br.read());
file.add(character);
}
BufferedWriter writer = new BufferedWriter(new FileWriter (new File ("file.lzw")));
//Declare temp variables
String encoded = "";
String temp = "";
//Loop through each character in the input file
for(int i = 0; i < file.size(); i++){
temp = temp + file.get(i); //Temp adds latest string
//Triggers when we reach a string that hasn"t been added to the key yet
if (!arr.contains(temp)){
//temp is always contained in our key if we take out the last letter, so we take out the last letter and add the key to the file
writer.write("" + arr.indexOf(temp.substring(0,temp.length()-1)) + " ");
arr.add(temp); //Add the new sequence to the key
temp = "" + file.get(i); //Reset temp to the most recent character
}
} if(!temp.equals("")) { //Handle the final temp
writer.write("" + arr.indexOf(temp));
}
writer.close();
br.close();
}
}