Author Topic: Helpfull Java Tutorials  (Read 164 times)

Keyani

  • Respect
  • Administrator
  • Newbie
  • ***
  • Posts: 48
  • Thanked +2/-0
  • Location: Belgium
    • View Profile
Helpfull Java Tutorials
« on: July 07, 2011, 06:20:42 pm »
Hai guys my friend thomas wrote these tutorials hes also know as mr. tom on rune-server.

FileWriter

Someone was asking me how to write player saving, so I figured what the hell, here is an example of how to do it.
Code: [Select]

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * This should help you understand everything you need to know about writing
 * files, such as player saving her I give you lots of examples of stuff you do,
 * just apply knowledge =).
 *
 * @author Sir Tom
 *
 */
public class WriteFile {

/** The class we are going to use to write the file for the example. **/
private BufferedWriter writer;

/** The name of the file. **/
public String fileName = "EXAMPLE_FILE";

/** Just an example array **/
private int[] array = { 1323, 3244, 55, 555, 3232, 767 };

/**
* This set's everything for writing data, and we actually write the data
* her
*
* @throws IOException
*             If an i/o error occurs.
*/
public WriteFile() throws IOException {
// write the actual file.
writer = new BufferedWriter(new FileWriter(fileName + ".txt"));

// now for this example i'm going to write a header for the file.
writer.write("[HEADER]");

// now we want to write on a different line so we do this.
writer.newLine();

// now lets write something for the fun of it.
writer.write("fileName - " + fileName);

// start a new line
writer.newLine();

// write an Integer
writer.write("Writing a Integer - "
+ Integer.toString(Integer.MAX_VALUE));

// start a new line
writer.newLine();

// write a Boolean
writer.write("Writing a Boolean - " + Boolean.toString(Boolean.TRUE));

// start a new line
writer.newLine();

// write a Long
writer.write("Writing a Long - " + Long.toString(Long.MAX_VALUE));

// start a new line
writer.newLine();

// write a Short
writer.write("Writing a Short - " + Short.toString(Short.MAX_VALUE));

// start a new line
writer.newLine();

// write a Float
writer.write("Writing a Float - " + Float.toString(Float.MAX_VALUE));

// start on a new line again.
writer.newLine();

// write a Double
writer.write("Writing a Double - " + Double.toString(Double.MAX_VALUE));

// lets write an array example
for (int i = 0; i < array.length; i++) {
writer.newLine();
writer.write("Array index: " + i + " value: "
+ Integer.toString(array[i]));
}

// new line
writer.newLine();

// new finish the header
writer.write("[/HEADER]");

// now we need to flush and close the writer.
writer.flush();
writer.close();

// now we are done.
System.out.println("Wrote all of the data to: " + fileName);
}

/**
* Start writing.
*
* @param strings
*            The command line arguments.
*/
public static void main(String... strings) {
try {
new WriteFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

File output:
Code: [Select]
[HEADER]
fileName - EXAMPLE_FILE
Writing a Integer - 2147483647
Writing a Boolean - true
Writing a Long - 9223372036854775807
Writing a Short - 32767
Writing a Float - 3.4028235E38
Writing a Double - 1.7976931348623157E308
Array index: 0 value: 1323
Array index: 1 value: 3244
Array index: 2 value: 55
Array index: 3 value: 555
Array index: 4 value: 3232
Array index: 5 value: 767
[/HEADER]
« Last Edit: July 07, 2011, 06:24:50 pm by Keyani »

Share on Bluesky Share on Facebook


Keyani

  • Respect
  • Administrator
  • Newbie
  • ***
  • Posts: 48
  • Thanked +2/-0
  • Location: Belgium
    • View Profile
Re: Helpfull Java Tutorials
« Reply #1 on: July 07, 2011, 06:21:52 pm »
Connecting to socket
Very very simple, in this I connect to runescape here just read the comment's

Code: [Select]
/**
 * Used if an exception is found that has to do with IO
 */
import java.io.IOException;

/**
 * The socket that we need to use
 */
import java.net.Socket;

/**
 * @author Tom The creator of this class
 *
 * @category Connector This is being used to connect to a socket
 *
 * @see Socket The class in which we are using
 */
public class SocketConnection {

/**
* The socket class in which is used to connect to a website for example
*/
public static Socket socket;

/**
* The main method of connecting to the socket and starting this program
*
* @param strings
*            The command line arguments
*
* @throws IOException
*             If an I/O exception is found
*/
public static void main(String... strings) throws IOException {

/**
* We instiniate the socket class to know to connect
*/
socket = new Socket(

/**
* The host we are connecting to
*/
"world1.runescape.com",

/**
* The port we are connecting to
*/
43594);

/**
* if we connected to the socket
*/
if (socket.isConnected()) {

/**
* send a message that we successfully connected
*/
System.out.println("Connected to socket: " + true);

/**
* else - if we get an error
*/

} else {

/**
* Send message that we couldn't connect
*/
System.out.println("Connected to socket: " + false);
}
}
}

« Last Edit: July 07, 2011, 06:23:30 pm by Keyani »

Keyani

  • Respect
  • Administrator
  • Newbie
  • ***
  • Posts: 48
  • Thanked +2/-0
  • Location: Belgium
    • View Profile
Re: Helpfull Java Tutorials
« Reply #2 on: July 07, 2011, 06:23:18 pm »
Things to know.

Final - can't be changed from the vaue it has been set with

Public - This can be used in any class

Private - Can only be used in the class declared

Static - Start will only be made once if that class has been made more than once

Packaging - Location of the class within the JVM[Java Virtual Machine]

Char[] - An array of character Objects

Modules - If you divide with a int and the int answer has a remander the modules[%] will give you the remainder

Pre-Increment - used by [++varible] or [--Variable] this will take the variable before used and add +1 to the it or -1

Post-Increment - used by [variable++] or [Variable--] this will take it and print the variable number then anything after will be +1 or -1

assignment operator - this is used to add to a int for example [monkey += 5;] this will add 4 to monkey [assignment operator] you can also do [-, *, /]

if statment - this is basicly a test for a variable [int/boolean] you can use if(youvariable ==[your check if its exactly the same] 2)] you can also use [!=(if not equals), <(less then varibale), >(greater then variable), <=(is it less then or equal to variable), >=(is it greater or equal to variable)] if it is it does one thing if it isnt it does another thing as specified by the body, if the variable isnt == to wat u want it to use } else { and it will tell java to do something different it can only do one at a time.

double amperstan[&&] - this is used for an if statment so if you want to do two ifs in one if you would use this for example [if(varibale == 10 && varibale == 10) {] then make it do something you can still use an else if

or[||] - used in a "if" statment [if(varible == 40 ||[or] varible == 50) {] only one has to be true and it does something if there both false you cant do what you want by using an } else { but one of them always has to be true or they both can be true

switch statment - used by [switch(varible) {] so what you would do is call a "case" like [case (case number) then a colan: ] so now basicly the case is an "if" so case = if so [case 4:] then make it do some code but you want to terminate what you are doing if it is that case so you would use a break [break;(semi colan)] this is used instead of so many if statments so basiclly [case 4: sendMessage("hi"); break;] so what that does is if the thing your switching is equal to 4 you would send the message "hi" so if your variable is equal to any case's you call use a default like [default:] then after that you want it to print the case not declared [System.out.println("didnt handle case "+variable);] then after you make the message after the default add a [break;]

While loop - this helps save time used by [while(varibale) {] this will execute this code after it in the brackets [ } { ] as long as you set it to so example [while(variable <[less then] 10) {] tell it to print "hi" but you want it to stop because it will go on so you would do [variable++;](post intcretment) wat it does is it tells it do 10 - 1 then stop
« Last Edit: July 07, 2011, 06:28:21 pm by Keyani »

Keyani

  • Respect
  • Administrator
  • Newbie
  • ***
  • Posts: 48
  • Thanked +2/-0
  • Location: Belgium
    • View Profile
Re: Helpfull Java Tutorials
« Reply #3 on: July 07, 2011, 06:24:14 pm »
Final, Static, Public, Private, Packaging
So quick meaning of them really just to tell people that don't really know what the mean
Code: [Select]
Final - can't be changed from the vaue it has been set with
Public - This can be used in any class
Private - Can only be used in the class declared
Static - Start will only be made once if that class has been made more than once
Packaging - Location of the class within the JVM[Java Virtual Machine]

Tom

  • Administrator
  • Newbie
  • ***
  • Posts: 12
  • Thanked +0/-0
    • View Profile
Re: Helpfull Java Tutorials
« Reply #4 on: July 07, 2011, 07:04:20 pm »
Thanks for this, massive help dude :)

Hayden

  • Forum Owner
  • Administrator
  • Member
  • ***
  • Posts: 77
  • Thanked +7/-0
    • View Profile
Re: Helpfull Java Tutorials
« Reply #5 on: July 08, 2011, 10:51:43 pm »
As always, great post. Keep it up.

Keyani

  • Respect
  • Administrator
  • Newbie
  • ***
  • Posts: 48
  • Thanked +2/-0
  • Location: Belgium
    • View Profile
Re: Helpfull Java Tutorials
« Reply #6 on: July 09, 2011, 09:47:07 am »
As always, great post. Keep it up.
Yh will do, il ask some friends for more.