SparseFloatMatrix2DSaver { public static void write(SparseFloatMatrix2D m, String filename) { File f = new File(filename); if (f.exists()) { f.renameTo(new File(f.getPath() + " backup " + new Date())); } try { Writer out = new FileWriter(filename); write(m, out); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } }  static void write(SparseFloatMatrix2D m, String filename); static void write(SparseFloatMatrix2D m, Writer w); static SparseFloatMatrix2D read(String filename); static SparseFloatMatrix2D read(Reader r); static String[] parse_header(String header_prefix, String input);  }
@Test public void test_write() { int nrows = 10; int ncolumns = 5; SparseFloatMatrix2D m = new SparseFloatMatrix2D(nrows, ncolumns); m.set(0, 0, 1); m.set(1, 1, 1); File f = new File("test_write"+ new Random().nextLong() + ".txt"); SparseFloatMatrix2DSaver.write(m, f.getPath()); try { BufferedReader in = new BufferedReader(new FileReader(f)); assertEquals("SparseFloatMatrix2D (" + nrows + "," + ncolumns +")", in.readLine()); assertEquals("0,0,1.0", in.readLine()); assertEquals("1,1,1.0", in.readLine()); assertEquals("SparseFloatMatrix2D END", in.readLine()); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); throw new AssertionError("This shouldn't happen"); } catch (IOException e) { e.printStackTrace(); throw new AssertionError("This shouldn't happen"); } finally { f.delete(); } }