Java - Downloading Wikipedia Articles This is my first experiment that uses a network API and connects to the internet. 
I tried to write a program that gives you an article of wikipedia directly by typing the name.

You can get an article like this:

java ArticleSupplier computer science
java ArticleSupplier captain underpants


If you have tips for me how to improve the performance or how to make a better structure, I would be thankful!

import java.util.Scanner;
import java.io.InputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

public class ArticleSupplier {
    private String nameOfArticle; // will be the name of the created document
    private String urlName;
    private URL url;
    private String article;

    public ArticleSupplier(String[] urlComponents) {
        // initialize article
        article = "";

        // initialize urlName and nameOfArticle
        urlName = "https://en.wikipedia.org/wiki/";
        nameOfArticle = "";
        for(int i = 0; i &lt; urlComponents.length; i++) {
            urlName += urlComponents[i];
            urlName += "_";

            nameOfArticle += urlComponents[i];
            nameOfArticle += " ";
        }
        urlName = urlName.substring(0, urlName.length() - 1);
        nameOfArticle = nameOfArticle.substring(0, nameOfArticle.length() - 1);

        // initialize url
        try {
            url = new URL(urlName);
        } catch(MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public void getArticle() {
        try(InputStream inputStream = url.openStream(); Scanner scanner = new Scanner(inputStream)) {
            while(scanner.hasNext()) {
                article += scanner.next() + "\n";
            }
        } catch(IOException e) {
            System.out.println(e);
        }
    }

    public void saveAsHTML() {
        String filename = nameOfArticle + ".html";
        try(FileWriter fileWriter = new FileWriter(filename);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
            bufferedWriter.write(article);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ArticleSupplier as = new ArticleSupplier(args);
        as.getArticle();
        as.saveAsHTML();
    }
}
Advice 1

urlName = "https://en.wikipedia.org/wiki/";


I suggest you make that magic string a static constant:

private static final String WIKIPEDIA_MAIN_URL =
        "https://en.wikipedia.org/wiki/";


Advice 2

for(int i = 0; i &lt; urlComponents.length; i++) {
    ...


According to (common) Java coding conventions there must be a single space between for and the opening brace:

for (int i ...
   ^


Advice 3

for (int i = 0; i &lt; urlComponents.length; i++) {


may be written as

for (String urlComponent : urlComponents) {


Advice 4

for(int i = 0; i &lt; urlComponents.length; i++) {
    urlName += urlComponents[i];
    urlName += "_";

    nameOfArticle += urlComponents[i];
    nameOfArticle += " ";
}
urlName = urlName.substring(0, urlName.length() - 1);
nameOfArticle = nameOfArticle.substring(0, nameOfArticle.length() - 1);


You are essentially repeating yourself; I would do this instead:

StringBuilder stringBuilder = new StringBuilder();
String separator = "";

for (String urlComponent : urlComponents) {
    stringBuilder.append(separator).append(urlComponent);
    separator = "_";
}

this.articleName = stringBuilder.toString();
String urlName = WIKIPEDIA_MAIN_URL + stringBuilder.toString();


Advice 5

public void getArticle() {
    try(InputStream inputStream = url.openStream(); Scanner scanner = new Scanner(inputStream)) {
        while(scanner.hasNext()) {
            article += scanner.next() + "\n";
        }
    } catch(IOException e) {
        System.out.println(e);
    }
}


Why read one "word" at a time when you can read an entire line. Also, whenever building such strings, StringBuilder will be much more efficient:

public void getArticle() {
    StringBuilder stringBuilder = new StringBuilder();

    try (Scanner scanner = new Scanner(url.openStream())) {
        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine()); // &lt;-- note the builder
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    this.article = stringBuilder.toString();
}


Alternative implementation

Gathering all the advices, I had this in mind:

import java.util.Scanner;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

public class ArticleSupplier {

    private static final String WIKIPEDIA_MAIN_URL =
            "https://en.wikipedia.org/wiki/";

    private String articleName;
    private URL url;
    private String article;

    public ArticleSupplier(String[] urlComponents) {
        StringBuilder stringBuilder = new StringBuilder();
        String separator = "";

        for (String urlComponent : urlComponents) {
            stringBuilder.append(separator).append(urlComponent);
            separator = "_";
        }

        this.articleName = stringBuilder.toString();
        String urlName = WIKIPEDIA_MAIN_URL + stringBuilder.toString();

        try {
            this.url = new URL(urlName);
        } catch(MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    public void getArticle() {
        StringBuilder stringBuilder = new StringBuilder();

        try (Scanner scanner = new Scanner(url.openStream())) {
            while (scanner.hasNextLine()) {
                stringBuilder.append(scanner.nextLine());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        this.article = stringBuilder.toString();
    }

    public void saveAsHTML() {
        String filename = articleName + ".html";

        try (BufferedWriter writer =
                new BufferedWriter(new FileWriter(filename))) {
            writer.write(article);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ArticleSupplier as = new ArticleSupplier(args);
        as.getArticle();
        as.saveAsHTML();
    }
}