Write JSON-formatted history file @param file
(File file)
| 244 | * @param file |
| 245 | */ |
| 246 | private static void writeListJSON(File file){ |
| 247 | if(!file.exists() || file.canWrite()){ |
| 248 | BufferedWriter writer = null; |
| 249 | |
| 250 | // create parent directories |
| 251 | File parentDir = file.getParentFile(); |
| 252 | if(!parentDir.exists()){ |
| 253 | parentDir.mkdirs(); |
| 254 | } |
| 255 | |
| 256 | try { |
| 257 | writer = new BufferedWriter(new FileWriter(file)); |
| 258 | } catch (IOException ex) { |
| 259 | Logger.getLogger(WorldFileList.class.getName()).log(Level.WARNING, null, ex); |
| 260 | } |
| 261 | |
| 262 | if(writer != null){ |
| 263 | JSONObject jRoot = new JSONObject(); |
| 264 | |
| 265 | jRoot.put("ver", "" + FILE_VER_MAJOR + "." + FILE_VER_MINOR); |
| 266 | |
| 267 | JSONArray jHistory = new JSONArray(); |
| 268 | jRoot.put("history", jHistory); |
| 269 | |
| 270 | for(WorldFileEntry entry: getEntries()){ |
| 271 | JSONObject jElement = new JSONObject(); |
| 272 | jHistory.put(jElement); |
| 273 | |
| 274 | jElement.put("file", entry.getFile().getAbsoluteFile()); |
| 275 | jElement.put("name", entry.getWorldName()); |
| 276 | } |
| 277 | |
| 278 | jRoot.write(writer, 4, 0); |
| 279 | |
| 280 | try { |
| 281 | writer.close(); |
| 282 | } catch (IOException ex) { |
| 283 | Logger.getLogger(WorldFileList.class.getName()).log(Level.SEVERE, null, ex); |
| 284 | } |
| 285 | } |
| 286 | } else { |
| 287 | System.err.println("Could not write file " + file.getAbsolutePath()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Read available worlds file (legacy) |
no test coverage detected