Nastya2018 |
10.08.2018 08:00 |
Про DataInputStream
Ребята как поменять код на DataInputStream. Чтобы памяти хватило когда создаю матрицу и умножаю миллион на миллион
Код:
public static void writeMatrixToFile(Matrix m, String filename) {
try(FileWriter writer = new FileWriter(filename, false)){
String strForWrite = "";
writer.write(String.valueOf(m.getRowCount()));
writer.write("\n");
writer.write(String.valueOf(m.getColCount()));
writer.write("\n");
for (int i = 1; i <= m.getRowCount(); i++){
for (int j = 1; j <= m.getColCount(); j++){
strForWrite += m.get(i,j) + " ";
}
strForWrite += "\n";
}
writer.write(strForWrite);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} catch(MatrixIndexException e) {
e.printStackTrace();
}
}
public static Matrix loadMatrixFromFile(String filename) {
try(FileReader reader = new FileReader(filename)) {
String strFromFile = "";
while (reader.ready()) {
strFromFile += (char) reader.read();
}
String[] arr = strFromFile.split(" |\n");
Matrix result = new Matrix1D(Integer.valueOf(arr[0]), Integer.valueOf(arr[1]));
int row = 1;
int col = 1;
for (int i = 2; i < arr.length; i++){
if (!arr[i].equals("")){
result.put(row, col, Integer.valueOf(arr[i]));
col++;
if (col == result.getColCount() + 1){
col = 1;
row++;
}
}
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
|