Click to See Complete Forum and Search --> : quite small (make the changelog non-static, i.e. load its content from a file)


Abdallah1
November 28th, 2009, 03:46 AM
To the Java developers,
I am currently having trouble with change the Changelogfrom hardcoded to a readout from a text file called "Changelog.txt".
So far i have made a class called ReadWriteChangelog.java
and here is the code i placed in: -

package org.freelords.forms.newgame;

import java.io.*;
import java.util.*;

import org.freelords.forms.newgame.ChangelogForm;

public class ReadWriteChangelog {


public static String getFileContents(String filename) throws IOException {

char[] buf = new char[512];
int len = 0;
StringBuilder builder = new StringBuilder();
FileReader reader = new FileReader(filename);
while (((len = reader.read(buf, 0, buf.length)) != -1))
{
builder.append(buf, 0, len);
}
String text = builder.toString();
BufferedReader buffReader = new BufferedReader(reader);
while (((len = buffReader.read(buf, 0, buf.length)) != -1))
{
builder.append(buf, 0, len);
}

char[] buffer = new char[512];
try
{
while (((len = buffReader.read(buf, 0, buf.length)) != -1))
{
builder.append(buf, 0, len);
}
}
finally
{
buffReader.close();
}
return builder.toString();
}
}

And what i did on the class ChangelogForm.java i tried to make the class read from the ReadWriteChangelog.java and that is what i written : -



public void init(Composite parent) {
super.init(parent);
File file = new File("I:\\eclipseproject\\eclipseFreelordsUI\\src\ \org\\freelords\\forms\\newgame\\Changelog.txt");

try {
//".\\FreelordsUI\\src\\org\\freelords\\forms\\newga me\\Changelog.txt"
Changelog = ReadWriteChangelog.getFileContents();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
description.setText(Changelog);

}
I can be able to operate the menu of the open source game but i can operate the Game very well but i can make the Changelog button to read from the "Changelog.txt". If you find a solution to this please reply to this thread or let me know by email.Thank you. :)

dlorde
November 28th, 2009, 06:05 PM
I don't see how that code compiles - the getFileContents method takes a String parameter, but you're trying to call it without any parameters.

If you're going to post code that runs, post the code that runs, otherwise it just wastes everyone's time.

You'll find a nicer way to read a text file in the Java Tutorials (http://java.sun.com/docs/books/tutorial/essential/io/file.html#readStream).

Bad code isn't bad, its just misunderstood...
Anon.