1515
1616import java .io .File ;
1717import java .io .IOException ;
18+ import java .nio .file .AtomicMoveNotSupportedException ;
19+ import java .nio .file .Files ;
20+ import java .nio .file .StandardCopyOption ;
1821import java .time .ZonedDateTime ;
1922import java .time .format .DateTimeFormatter ;
2023import java .util .Locale ;
@@ -51,10 +54,16 @@ public void save(File folder) {
5154 if (folder != null ) file = new File (folder , file .getName ());
5255
5356 file .getParentFile ().mkdirs ();
54- StreamUtils .copy (tempFile , file );
57+
58+ try {
59+ Files .move (tempFile .toPath (), file .toPath (), StandardCopyOption .REPLACE_EXISTING , StandardCopyOption .ATOMIC_MOVE );
60+ } catch (AtomicMoveNotSupportedException e ) {
61+ StreamUtils .copy (tempFile , file );
62+ }
63+
5564 tempFile .delete ();
5665 } catch (IOException e ) {
57- e . printStackTrace ( );
66+ MeteorClient . LOG . error ( "Error saving {}. Possibly corrupted?" , this . name , e );
5867 }
5968 }
6069
@@ -75,14 +84,19 @@ public void load(File folder) {
7584 } catch (CrashException e ) {
7685 String backupName = FilenameUtils .removeExtension (file .getName ()) + "-" + ZonedDateTime .now ().format (DATE_TIME_FORMATTER ) + ".backup.nbt" ;
7786 File backup = new File (file .getParentFile (), backupName );
78- StreamUtils .copy (file , backup );
79- MeteorClient .LOG .error ("Error loading {}. Possibly corrupted?" , this .name );
87+
88+ try {
89+ Files .move (file .toPath (), backup .toPath (), StandardCopyOption .REPLACE_EXISTING , StandardCopyOption .ATOMIC_MOVE );
90+ } catch (AtomicMoveNotSupportedException ex ) {
91+ StreamUtils .copy (file , backup );
92+ }
93+
94+ MeteorClient .LOG .error ("Error loading {}. Possibly corrupted?" , this .name , e );
8095 MeteorClient .LOG .info ("Saved settings backup to '{}'." , backup );
81- e .printStackTrace ();
8296 }
8397 }
8498 } catch (IOException e ) {
85- e . printStackTrace ( );
99+ MeteorClient . LOG . error ( "Error loading {}. Possibly corrupted?" , this . name , e );
86100 }
87101 }
88102
0 commit comments