77import org .schabi .newpipe .extractor .downloader .Response ;
88import org .schabi .newpipe .extractor .exceptions .ReCaptchaException ;
99
10- import java .io .File ;
11- import java .io .FileOutputStream ;
1210import java .io .IOException ;
13- import java .io .OutputStreamWriter ;
14- import java .nio .charset .StandardCharsets ;
1511import java .nio .file .Files ;
16- import java .nio .file .Path ;
1712import java .nio .file .Paths ;
1813
1914import javax .annotation .Nonnull ;
@@ -53,12 +48,13 @@ class RecordingDownloader extends Downloader {
5348 */
5449 public RecordingDownloader (final String stringPath ) throws IOException {
5550 this .path = stringPath ;
56- final Path path = Paths .get (stringPath );
57- final File folder = path .toFile ();
58- if (folder .exists ()) {
59- for (final File file : folder .listFiles ()) {
60- if (file .getName ().startsWith (RecordingDownloader .FILE_NAME_PREFIX )) {
61- file .delete ();
51+ final var path = Paths .get (stringPath );
52+ if (Files .exists (path )) {
53+ try (final var directoryStream = Files .newDirectoryStream (path ,
54+ entry -> entry .getFileName ().toString ()
55+ .startsWith (RecordingDownloader .FILE_NAME_PREFIX ))) {
56+ for (final var entry : directoryStream ) {
57+ Files .delete (entry );
6258 }
6359 }
6460 } else {
@@ -80,18 +76,14 @@ public Response execute(@Nonnull final Request request) throws IOException,
8076 response .latestUrl ()
8177 );
8278
83- final File outputFile = new File (path + File .separator + FILE_NAME_PREFIX + index
84- + ".json" );
79+ final var outputPath = Paths .get (path ).resolve (FILE_NAME_PREFIX + index + ".json" );
8580 index ++;
86- outputFile .createNewFile ();
87- final OutputStreamWriter writer = new OutputStreamWriter (new FileOutputStream (outputFile ),
88- StandardCharsets .UTF_8 );
89- new GsonBuilder ()
90- .setPrettyPrinting ()
91- .create ()
92- .toJson (new TestRequestResponse (request , response ), writer );
93- writer .flush ();
94- writer .close ();
81+ try (final var writer = Files .newBufferedWriter (outputPath )) {
82+ new GsonBuilder ()
83+ .setPrettyPrinting ()
84+ .create ()
85+ .toJson (new TestRequestResponse (request , response ), writer );
86+ }
9587
9688 return response ;
9789 }
0 commit comments