|
4 | 4 | import static xdean.jex.util.lang.ExceptionUtil.uncheck; |
5 | 5 |
|
6 | 6 | import java.io.IOException; |
| 7 | +import java.nio.file.DirectoryStream; |
7 | 8 | import java.nio.file.Files; |
8 | 9 | import java.nio.file.Path; |
9 | 10 | import java.nio.file.Paths; |
@@ -45,24 +46,26 @@ public void bind(Stage stage) { |
45 | 46 | public void open() { |
46 | 47 | try { |
47 | 48 | FileUtil.createDirectory(LAST_FILE_PATH); |
48 | | - Files.newDirectoryStream(LAST_FILE_PATH, "*.tmp").forEach(p -> uncheck(() -> { |
49 | | - List<String> lines = Files.readAllLines(p, options.charset().getValue()); |
50 | | - if (lines.isEmpty()) { |
51 | | - return; |
52 | | - } |
53 | | - String head = lines.get(0); |
54 | | - CssEditor editor = editorFactory.get(); |
55 | | - editor.fileProperty().set(Try.to(() -> Integer.valueOf(head)).map(i -> FileWrapper.newFile(i)) |
56 | | - .getOrElse(() -> FileWrapper.existFile(Paths.get(head)))); |
57 | | - lines.stream() |
58 | | - .skip(1) |
59 | | - .reduce((a, b) -> String.join(System.lineSeparator(), a, b)) |
60 | | - .ifPresent(t -> { |
61 | | - editor.replaceText(t); |
62 | | - editor.getUndoManager().forgetHistory(); |
63 | | - }); |
64 | | - contextService.editorList().add(editor); |
65 | | - })); |
| 49 | + try (DirectoryStream<Path> stream = Files.newDirectoryStream(LAST_FILE_PATH, "*.tmp")) { |
| 50 | + stream.forEach(p -> uncheck(() -> { |
| 51 | + List<String> lines = Files.readAllLines(p, options.charset().getValue()); |
| 52 | + if (lines.isEmpty()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + String head = lines.get(0); |
| 56 | + CssEditor editor = editorFactory.get(); |
| 57 | + editor.fileProperty().set(Try.to(() -> Integer.valueOf(head)).map(i -> FileWrapper.newFile(i)) |
| 58 | + .getOrElse(() -> FileWrapper.existFile(Paths.get(head)))); |
| 59 | + lines.stream() |
| 60 | + .skip(1) |
| 61 | + .reduce((a, b) -> String.join(System.lineSeparator(), a, b)) |
| 62 | + .ifPresent(t -> { |
| 63 | + editor.replaceText(t); |
| 64 | + editor.getUndoManager().forgetHistory(); |
| 65 | + }); |
| 66 | + contextService.editorList().add(editor); |
| 67 | + })); |
| 68 | + } |
66 | 69 | } catch (IOException e) { |
67 | 70 | dialogService.errorDialog(e).content("Fail to open last files.").show(); |
68 | 71 | } |
|
0 commit comments