-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.java
More file actions
105 lines (105 loc) · 2.47 KB
/
FileIO.java
File metadata and controls
105 lines (105 loc) · 2.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
public class FileIO{
char c;
FileReader reader;
PrintWriter writer;
BufferedReader buff;
CharList list;
String nameOfFile;
FileIO(String filename){
nameOfFile = filename;
list = new CharList();
}
void openInput(){
try{
reader = new FileReader(nameOfFile);
}
catch(IOException e){
System.out.println("Error Reading File");
}
}
void openOutput(){
try{
writer = new PrintWriter(nameOfFile);
}
catch(IOException e){
System.out.println("Error Reading File");
}
}
CharList importFileText(){
int i;
try{
while((i=reader.read()) != -1){
c = (char) i;
list.add(c);
}
}
catch(IOException e){
System.out.println("Error Reading File at FileIO.java Line:25");
}
return list;
}
void refresh(){
try{
reader.close();
}
catch(IOException e){
System.out.println("Error Reading File");
}
}
String allText(){
refresh();
String contents = "";
String temp;
try{
reader = new FileReader(nameOfFile);
buff = new BufferedReader(reader);
while((temp = buff.readLine()) != null){
contents += temp;
}
}
catch(IOException e){
System.out.println("Error Reading the File.");
}
return contents;
}
void createChart(BitMap map){
refresh();
String temp;
try{
reader = new FileReader("EncodingChart.txt");
buff = new BufferedReader(reader);
while((temp = buff.readLine()) != null){
temp = temp.replaceAll("\\s+",",");
String tokens[] = temp.split(",");
for(String s:tokens){
String c = s.substring(0,s.indexOf("-"));
if(c.equals("pad")){
map.pad = s.substring((s.indexOf("-")+1));
}
else{
int i = Integer.parseInt(c);
map.encoding[i] = s.substring((s.indexOf("-")+1));
}
}
}
}
catch(IOException e){
System.out.println("Error Reading the File.");
}
}
public void PrintOutput(String cText,BitMap map){
//print the characters with their respective huffman encoding
for(char c: map.keySet()){
writer.print(c+map.get(c)+">");
System.out.println(c+map.get(c)+">");
}
//add the base64 converted text
writer.print("#"+cText);
System.out.println("#"+cText);
writer.close();
}
}