-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManagement.java
More file actions
94 lines (75 loc) · 2.26 KB
/
MemoryManagement.java
File metadata and controls
94 lines (75 loc) · 2.26 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.StringTokenizer;
public class MemoryManagement {
public static void main(String[] args) {
boolean useLru = args[0].matches("1");
String inputFilename = args[1];
String outputFilename = args[2];
int mainMemorySize = Integer.parseInt(args[3]);
int secondaryMemorySize = Integer.parseInt(args[4]);
MemoryManagementSystem memory = new MemoryManagementSystem(mainMemorySize, secondaryMemorySize, useLru);
File inFile = new File(inputFilename);
File outFile = new File(outputFilename);
try {
Files.deleteIfExists(outFile.toPath());
} catch (IOException e) {
e.printStackTrace();
}
manageMemory(inFile, outFile, memory);
}
public static void manageMemory(File inFile,
File outFile, MemoryManagementSystem memory) {
try {
FileReader ifr = new FileReader(inFile);
BufferedReader ibr = new BufferedReader(ifr);
String line = "";
while (line != null) {
line = ibr.readLine();
if (line != null) {
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
String token = st.nextToken().trim();
if (token.equals("read")) {
memory.read(Integer.parseInt(st.nextToken().trim()));
}
if (token.equals("write")) {
int index = Integer.parseInt(st.nextToken().trim());
char c = st.nextToken().trim().charAt(0);
memory.write(index, c);
}
if (token.equals("print")) {
writeToFile(outFile, memory.toString());
}
}
}
}
ibr.close();
ifr.close();
}
catch (Exception e) {
System.out.println("Error \"" + e.toString() + "\" on file "
+ outFile.getPath());
e.printStackTrace();
System.exit(-1); // brutally exit the program
}
}
private static void writeToFile(File outFile, String toWrite) {
try {
FileWriter ofw = new FileWriter(outFile, true);
// Writing to file
ofw.append(toWrite);
ofw.append("\n");
ofw.close();
} catch (Exception e) {
System.out.println("Error \"" + e.toString() + "\" on file "
+ outFile.getPath());
e.printStackTrace();
System.exit(-1); // brutally exit the program
}
}
}