-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encode.java
54 lines (46 loc) · 1.3 KB
/
Encode.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
//Vincent Tran
//CS 1501
//Will perform Move to Front transform on ASCII characters
//Format: java Encode inputfilename.extension outputfilename.extension
//Tested files: .bin
import java.lang.*;
import java.util.*;
import java.io.*;
public class Encode
{
public static void main(String [] args) throws IOException
{
ArrayList<Character> data = new ArrayList<Character>();
int[] dict = new int[256];
for(int i = 0; i < 256; i++)
dict[i] = i;
String current;
//Will read each character from the input file and store into an ArrayList
BufferedReader br = new BufferedReader(new FileReader(args[0]));
current = br.readLine();
for(int i=0; i < current.length(); i++)
data.add(current.charAt(i));
int index=0;
int charVal;
String output = "";
//Performs the Move-To-Front transform on the ArrayList
for(char x : data)
{
charVal = (int) x;
for(int i = 0; i < 256; i++)
{
if(dict[i] == charVal)
index = i;
}
for(int check = index; check != 0; check--)
dict[check] = dict[check-1];
dict[0] = charVal;
output = output + index + " ";
}
//Writes into a file with the user-defined name and extension
File file = new File(args[1]);
Writer fileOut = new BufferedWriter(new FileWriter(file));
fileOut.write(output);
fileOut.close();
}
}