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.
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:
[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]