Fix bugs and realize writing file.