10.08.2018, 08:00 | #1 (permalink) |
Новичок
Регистрация: 10.08.2018
Сообщений: 1
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 10
|
Про 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; } } |
Ads | |
Member
Регистрация: 31.10.2006
Сообщений: 40200
Записей в дневнике: 0
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 55070
|
|
|