Save the content of a String into a file - Save the content into a temp file - Find the canonical path of the file (if it's a symlink, follow it) - Remove the original file - Move temp file to original path This ensures that the file is not getting truncated if the disk is full
(String str, File file)
| 883 | * This ensures that the file is not getting truncated if the disk is full |
| 884 | */ |
| 885 | static public void saveFile(String str, File file) throws IOException { |
| 886 | File temp = File.createTempFile(file.getName(), null, file.getParentFile()); |
| 887 | // Split the file content using minimum common separator \n |
| 888 | // then trim any other character (\r) so saveStrings can print it in the correct |
| 889 | // format for every OS |
| 890 | String strArray[] = str.split("\n"); |
| 891 | for (String item : strArray) { |
| 892 | item.trim(); |
| 893 | } |
| 894 | PApplet.saveStrings(temp, strArray); |
| 895 | |
| 896 | try { |
| 897 | file = file.toPath().toRealPath().toFile().getCanonicalFile(); |
| 898 | } catch (IOException e) { |
| 899 | } |
| 900 | |
| 901 | if (file.exists()) { |
| 902 | boolean result = file.delete(); |
| 903 | if (!result) { |
| 904 | throw new IOException( |
| 905 | I18n.format( |
| 906 | tr("Could not remove old version of {0}"), |
| 907 | file.getAbsolutePath())); |
| 908 | } |
| 909 | } |
| 910 | boolean result = temp.renameTo(file); |
| 911 | if (!result) { |
| 912 | throw new IOException( |
| 913 | I18n.format( |
| 914 | tr("Could not replace {0}"), |
| 915 | file.getAbsolutePath())); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | static public void selectBoard(TargetBoard targetBoard) { |
| 920 | TargetPlatform targetPlatform = targetBoard.getContainerPlatform(); |