44import org .bukkit .Bukkit ;
55import org .bukkit .plugin .Plugin ;
66
7- import java .io .BufferedReader ;
8- import java .io .File ;
9- import java .io .IOException ;
10- import java .io .InputStreamReader ;
7+ import java .io .*;
118import java .net .URL ;
129import java .net .URLConnection ;
1310import java .net .UnknownHostException ;
@@ -279,7 +276,7 @@ public static boolean isConnected() {
279276 conn .connect ();
280277 conn .getInputStream ().close ();
281278 return true ;
282- } catch (Exception var2 ) {
279+ } catch (IOException var2 ) {
283280 return false ;
284281 }
285282 }
@@ -294,13 +291,31 @@ public static String readWithInputStream(String url) {
294291 URL javaURL = new URL (url );
295292 URLConnection connection = javaURL .openConnection ();
296293 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 ){
299296 ex .printStackTrace ();
300297 return null ;
301298 }
302299 }
303300
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+
304319 /* Maths */
305320
306321 /**
@@ -492,48 +507,50 @@ public static File folder(File folder) {
492507 * Used to read a file into string
493508 * @param file File to read
494509 * @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
496511 */
497- public static String readFile (File file ) throws Exception {
512+ public static String readFile (File file ) throws IOException {
498513 return FileUtils .readFileToString (file , Charset .defaultCharset ());
499514 }
500515
501516 /**
502517 * Reads all the lines of a file
503518 * @param file File to read lines
504519 * @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
506521 */
507- public static List <String > readLines (File file )throws Exception {
522+ public static List <String > readLines (File file )throws IOException {
508523 return FileUtils .readLines (file , Charset .defaultCharset ());
509524 }
510525
511526 /**
512527 * Writes a set of lines into a file
513528 * @param file File to write
514529 * @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
516531 */
517- public static void writeFile (File file , Collection <String > data ) throws Exception {
532+ public static void writeFile (File file , Collection <String > data ) throws IOException {
518533 FileUtils .writeLines (file , data );
519534 }
520535
521536 /**
522537 * Used to write a string into a file
523538 * @param file File to write the data
524539 * @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
526541 */
527- public static void writeFile (File file , String data )throws Exception {
542+ public static void writeFile (File file , String data ) throws IOException {
528543 FileUtils .write (file , data , Charset .defaultCharset ());
529544 }
530545
531546 /**
532547 * Used to destroy files
533548 * @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
535552 */
536- public static void destroyFile (File file ) throws Exception {
553+ public static void destroyFile (File file ) throws IOException {
537554 FileUtils .forceDelete (file );
538555 }
539556
@@ -543,12 +560,12 @@ public static void destroyFile(File file) throws Exception{
543560 * @param destination File to save the downloaded file
544561 * @throws UnknownHostException if is not connected to internet
545562 * @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
547564 * @throws IOException if the destination file cannot be written
548565 * @throws IOException if the destination file needs creating but can't be
549566 * @throws IOException if an IO error occurs during copying
550567 */
551- public static void downloadFile (String url , File destination ) throws Exception {
568+ public static void downloadFile (String url , File destination ) throws IOException {
552569 if (!isConnected ()) throw new UnknownHostException ("Cannot connect to internet!" );
553570 FileUtils .copyURLToFile (new URL (url ), destination );
554571 }
@@ -560,12 +577,12 @@ public static void downloadFile(String url, File destination) throws Exception{
560577 * @param fileName Name of the downloaded file
561578 * @throws UnknownHostException if is not connected to internet
562579 * @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
564581 * @throws IOException if the destination file cannot be written
565582 * @throws IOException if the destination file needs creating but can't be
566583 * @throws IOException if an IO error occurs during copying
567584 */
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 {
569586 downloadFile (url , new File (folder (outputFolder ), fileName ));
570587 }
571588
0 commit comments