-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMemory.java
More file actions
68 lines (53 loc) · 1.62 KB
/
MainMemory.java
File metadata and controls
68 lines (53 loc) · 1.62 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
package OperatingSystem;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class MainMemory {
private String DIVIDER = "~";
public int SIZE_OF_BLOCK = 4;
private String arrayMain[];
private File file;
public MainMemory(int size) {
arrayMain = new String[size];
for (int i = 0; i < size; i++)
arrayMain[i] = generateRandomString();
file = new File("FILE_SYSTEM/persistentMainMemory.txt");
try {
if (!file.exists()) {
file.createNewFile();
} else {
String content = FileUtils.readFileToString(file);
for (int i = 0; i < content.split(DIVIDER).length; i++)
arrayMain[i] = content.split(DIVIDER)[i];
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeToMemory(int block, int position, String stringToWrite) {
String content = readFromMemory(block);
StringBuilder builder = new StringBuilder(content);
builder.insert(position, stringToWrite);
arrayMain[block] = builder.substring(0, SIZE_OF_BLOCK);
builder = new StringBuilder();
for (String s : arrayMain)
builder.append(s + DIVIDER);
builder.deleteCharAt(builder.length() - 1); /* delete last DIVIDER */
try {
FileUtils.write(file, builder.toString(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
public String readFromMemory(int block) {
return arrayMain[block];
}
private String generateRandomString() {
String word = "";
for (int i = 0; i < SIZE_OF_BLOCK; i++) {
// word += (char) ('A' + new Random().nextInt(26));
word += "\0";
}
return word;
}
}