4
4
import org .bukkit .Bukkit ;
5
5
import org .bukkit .plugin .Plugin ;
6
6
7
- import java .io .BufferedReader ;
8
- import java .io .File ;
9
- import java .io .IOException ;
10
- import java .io .InputStreamReader ;
7
+ import java .io .*;
11
8
import java .net .URL ;
12
9
import java .net .URLConnection ;
13
10
import java .net .UnknownHostException ;
@@ -279,7 +276,7 @@ public static boolean isConnected() {
279
276
conn .connect ();
280
277
conn .getInputStream ().close ();
281
278
return true ;
282
- } catch (Exception var2 ) {
279
+ } catch (IOException var2 ) {
283
280
return false ;
284
281
}
285
282
}
@@ -294,13 +291,31 @@ public static String readWithInputStream(String url) {
294
291
URL javaURL = new URL (url );
295
292
URLConnection connection = javaURL .openConnection ();
296
293
connection .setRequestProperty ("User-Agent" , "Mozilla/5.0" );
297
- return new BufferedReader (new InputStreamReader (connection .getInputStream ())).readLine ( );
298
- }catch (Exception ex ){
294
+ return new BufferedReader (new InputStreamReader (connection .getInputStream ())).lines (). collect ( Collectors . joining () );
295
+ }catch (IOException ex ){
299
296
ex .printStackTrace ();
300
297
return null ;
301
298
}
302
299
}
303
300
301
+ /**
302
+ * Used to read a whole website returning a list of every line
303
+ *
304
+ * @param url URL of the website
305
+ * @return Content of the website
306
+ */
307
+ public static List <String > readLinesWithInputStream (String url ){
308
+ try {
309
+ URL webURL = new URL (url );
310
+ URLConnection connection = webURL .openConnection ();
311
+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
312
+ return reader .lines ().collect (Collectors .toList ());
313
+ }catch (IOException e ){
314
+ e .printStackTrace ();
315
+ return new ArrayList <>();
316
+ }
317
+ }
318
+
304
319
/* Maths */
305
320
306
321
/**
@@ -492,48 +507,50 @@ public static File folder(File folder) {
492
507
* Used to read a file into string
493
508
* @param file File to read
494
509
* @return Content of the file
495
- * @throws Exception If occurs any exception on the file reading
510
+ * @throws IOException If occurs any exception on the file reading
496
511
*/
497
- public static String readFile (File file ) throws Exception {
512
+ public static String readFile (File file ) throws IOException {
498
513
return FileUtils .readFileToString (file , Charset .defaultCharset ());
499
514
}
500
515
501
516
/**
502
517
* Reads all the lines of a file
503
518
* @param file File to read lines
504
519
* @return Lines of the file
505
- * @throws Exception If occurs any exception on the file reading
520
+ * @throws IOException If occurs any exception on the file reading
506
521
*/
507
- public static List <String > readLines (File file )throws Exception {
522
+ public static List <String > readLines (File file )throws IOException {
508
523
return FileUtils .readLines (file , Charset .defaultCharset ());
509
524
}
510
525
511
526
/**
512
527
* Writes a set of lines into a file
513
528
* @param file File to write
514
529
* @param data Data to write into the file
515
- * @throws Exception If occurs any exception on the file writing
530
+ * @throws IOException If occurs any exception on the file writing
516
531
*/
517
- public static void writeFile (File file , Collection <String > data ) throws Exception {
532
+ public static void writeFile (File file , Collection <String > data ) throws IOException {
518
533
FileUtils .writeLines (file , data );
519
534
}
520
535
521
536
/**
522
537
* Used to write a string into a file
523
538
* @param file File to write the data
524
539
* @param data Data to write into the file
525
- * @throws Exception If occurs any exception on the file writing
540
+ * @throws IOException If occurs any exception during the file writing
526
541
*/
527
- public static void writeFile (File file , String data )throws Exception {
542
+ public static void writeFile (File file , String data ) throws IOException {
528
543
FileUtils .write (file , data , Charset .defaultCharset ());
529
544
}
530
545
531
546
/**
532
547
* Used to destroy files
533
548
* @param file File to destroy
534
- * @throws Exception If occurs any exception on file deletion
549
+ * @throws NullPointerException if the directory is null
550
+ * @throws FileNotFoundException if the file was not found
551
+ * @throws IOException in case deletion is unsuccessful
535
552
*/
536
- public static void destroyFile (File file ) throws Exception {
553
+ public static void destroyFile (File file ) throws IOException {
537
554
FileUtils .forceDelete (file );
538
555
}
539
556
@@ -543,12 +560,12 @@ public static void destroyFile(File file) throws Exception{
543
560
* @param destination File to save the downloaded file
544
561
* @throws UnknownHostException if is not connected to internet
545
562
* @throws IOException if the URL cannot be opened
546
- * @throws IOException if the is a directory
563
+ * @throws IOException if the destination is a directory
547
564
* @throws IOException if the destination file cannot be written
548
565
* @throws IOException if the destination file needs creating but can't be
549
566
* @throws IOException if an IO error occurs during copying
550
567
*/
551
- public static void downloadFile (String url , File destination ) throws Exception {
568
+ public static void downloadFile (String url , File destination ) throws IOException {
552
569
if (!isConnected ()) throw new UnknownHostException ("Cannot connect to internet!" );
553
570
FileUtils .copyURLToFile (new URL (url ), destination );
554
571
}
@@ -560,12 +577,12 @@ public static void downloadFile(String url, File destination) throws Exception{
560
577
* @param fileName Name of the downloaded file
561
578
* @throws UnknownHostException if is not connected to internet
562
579
* @throws IOException if the URL cannot be opened
563
- * @throws IOException if the is a directory
580
+ * @throws IOException if the destination is a directory
564
581
* @throws IOException if the destination file cannot be written
565
582
* @throws IOException if the destination file needs creating but can't be
566
583
* @throws IOException if an IO error occurs during copying
567
584
*/
568
- public static void downloadFile (String url , File outputFolder , String fileName )throws Exception {
585
+ public static void downloadFile (String url , File outputFolder , String fileName ) throws IOException {
569
586
downloadFile (url , new File (folder (outputFolder ), fileName ));
570
587
}
571
588
0 commit comments