-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.java
More file actions
126 lines (94 loc) · 3.56 KB
/
FileSystem.java
File metadata and controls
126 lines (94 loc) · 3.56 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
package code_graph_generator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class FileSystem {
private String fileName;
private ArrayList<String> fileRows;
private int numOfMatrices;
public FileSystem(String file) {
fileName = file;
fileRows = new ArrayList<String>();
this.numOfMatrices = 0;
readFile(fileName);
}
public void readFile(String fileName) {
String line = null;
int counter = 0;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
fileRows.add(line);
if (line.contentEquals("")) counter++;
}
// Always close files.
bufferedReader.close();
this.numOfMatrices = counter + 1;
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
}
}
public void writeFile(String fileName, String temp) {
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(temp);
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
}
}
public ArrayList<String> getFileRows() {
return fileRows;
}
public int[][] getAdjMatrix() {
int[][] adjMatrix = findMatrix(fileRows, 0);
return adjMatrix;
}
public int[][] getBandwidthMatrix() {
int[][] linkMatrix = findMatrix(fileRows, 1);
return linkMatrix;
}
public int[][] getDelayMatrix() {
int[][] delayMatrix = findMatrix(fileRows, 2);
return delayMatrix;
}
public int[][] getReliabilityMatrix() {
int[][] reliabilityMatrix = findMatrix(fileRows, 3);
return reliabilityMatrix;
}
public int[][] getSpectrumStartIndex() {
int[][] spectrumStartIndex = findMatrix(fileRows, 4);
return spectrumStartIndex;
}
public int[][] findMatrix(ArrayList<String> fileRows, int type) {
int[][] temp;
String row[];
int start;
temp = new int[fileRows.size() / numOfMatrices][fileRows.size() / numOfMatrices];
row = new String[fileRows.size() / numOfMatrices];
start = type * fileRows.size() / numOfMatrices;
if (type != 0) start = start + 1;
for (int i = start; i < start + fileRows.size() / numOfMatrices; i++) {
if (fileRows.get(i).length() != 0) {
row = fileRows.get(i).split("\t");
for (int j = 0; j < row.length; j++) {
temp[i - start][j] = Integer.valueOf(row[j]);
}
}
}
return temp;
}
}