Java Tutorial

From Global Shellz Wiki
Jump to: navigation, search

I am assuming that you can at least install Eclipse or Netbeans and can make a new project to follow this tutorial.

Contents

Hello World

To begin with the simple hello world, you are going to use the following code.


public class HelloShell {

	public static void main(String[] args) {

		System.out.println("Hello Global Shellz");
		//Simply sends, Hello Global Shellz, to the console

	}

}

That is the most basic hello world example that you will come across. It shows the basics of writing out to the console for the user to read.

User input

What about taking user input? To do this we will use something called a scanner. It will get the next segment of code and do what you want with it. Here is how.



import java.util.Scanner;
//Imports "Scanner"

public class HelloShell {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String STRING_NAME = new String();
			//Declaring "STRING_NAME" as a new string.
			//Declaring "scan" as a new scanner.
		
		STRING_NAME = scan.nextLine();
			//Sets the String "STRING_NAME" equal to what ever you type into the console.
		
		System.out.println(STRING_NAME);
			//Prints out what ever "scan" grabbed and outputs it to the console.
	}

}


Download

I will jump into something a bit more interesting and show you how to download something. For this example it will download the Google logo and save it to a directory of your choice.


import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
//Imports stuff to download and url connect.

public class HelloShell {

	public static void main(String[] args) {

		final String SAVE_TO = new String("/Directory/To/Save/To/");
		final String URL_DOWNLOAD = new String("http://www.google.com/images/logos/ps_logo2.png");
		final String FILE_NAME = new String("GoogleLogo.png");
		//Declaring the strings for download, the output location, and the file name to save it as.
		
		final int BYTE_SIZE = 1;
		//This is the amount of bytes you want to download at a time until the download is finished.

		try {
			           
            byte[] a = new byte[BYTE_SIZE];
            URL urla = new URL(URL_DOWNLOAD);
            URLConnection urlConnectiona = urla.openConnection();
            urlConnectiona.connect();
            DataInputStream dia = new DataInputStream(urlConnectiona.getInputStream());
            FileOutputStream foa = new FileOutputStream(SAVE_TO + FILE_NAME);
            while (-1 != dia.read(a, 0, BYTE_SIZE))
                foa.write(a, 0, BYTE_SIZE);
            dia.close();
            foa.close();
        } catch (IOException e) {
            e.printStackTrace();
            //ERROR OUT
        }
        
	}

}

You need to change the strings at the top to fit your needs and change the byte size to something respectable to the file size and the system specs. For a test to see what you have learned, see if you can incorporate the scanner so that you can input the URL, File Name, and the Output location, and maybe even the byte size if you so choose.

External links

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox