-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.java
More file actions
136 lines (112 loc) · 3.65 KB
/
FileOperations.java
File metadata and controls
136 lines (112 loc) · 3.65 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.Scanner ;
import java.util.ArrayList ;
import java.io.ObjectOutputStream ;
import java.io.FileOutputStream ;
import java.io.ObjectInputStream ;
import java.io.FileInputStream ;
import java.io.File ;
import java.io.BufferedReader ;
import java.io.BufferedWriter ;
import java.io.FileReader ;
import java.io.FileWriter ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
public class FileOperations
{
private static FileInputStream byteInput = null;
public Scanner openFile (String file_name)
{
/**
* This method generates a file reference to read file line by line
* and returns this reference to where the method called is.
*/
Scanner file_reference = null ;
try
{
file_reference = new Scanner (new BufferedReader (new FileReader (new File (file_name)))) ;
}
catch (FileNotFoundException ex)
{
System.out.println (file_name + " could not found.!") ;
ex.printStackTrace () ;
}
return file_reference ;
}
public String readFile (Scanner file)
{
/**
* This method read file line by line
* and returns the line.
*/
String line = null ;
if (file.hasNextLine ())
{
line = file.nextLine () ;
}
return line ;
}
public void writeFile (String line , String file_name)
{
/**
* This method writes line which passed as paramater to the file
* which determined as parameter.
*/
try (BufferedWriter writer = new BufferedWriter (new FileWriter (new File (file_name) , true)))
{
writer.write (line + '\n') ;
}
catch (IOException ex)
{
System.out.println ("Could not write to the " + file_name + ".!") ;
ex.printStackTrace () ;
}
}
public void writeCodes (ArrayList <Node <Character , Integer>> list , String file_name)
{
/**
* This method writes array list object to the file.
* Reason of this is that for using frequency list in the decoding operation.
*/
try (ObjectOutputStream output = new ObjectOutputStream (new FileOutputStream (new File (file_name))))
{
output.writeObject (list) ;
}
catch (FileNotFoundException ex)
{
System.out.println (file_name + " could not found.!") ;
ex.printStackTrace () ;
}
catch (IOException ex)
{
System.out.println ("Could not write to the " + file_name + ".!") ;
ex.printStackTrace () ;
}
}
public ArrayList<Node<Character, Integer>> readCodes (String file_name)
{
/**
* This method reads array list to the file which determined as parameter.
* Reading operation is used for decoding operation.
*/
ArrayList <Node <Character , Integer>> codes = null ;
try (ObjectInputStream input = new ObjectInputStream (new FileInputStream (new File (file_name))))
{
codes = (ArrayList <Node <Character , Integer>>) input.readObject () ;
}
catch (FileNotFoundException ex)
{
System.out.println (file_name + " could not found.!") ;
ex.printStackTrace () ;
}
catch (ClassNotFoundException ex)
{
System.out.println ("Node class could not found.!") ;
ex.printStackTrace () ;
}
catch (IOException ex)
{
System.out.println ("Could not read from " + file_name + ".!") ;
}
return codes ;
}
}