Page 1 of 1
Reading & writing an external text file?
Posted: Mon Aug 27, 2007 11:23 am
by SunTzu
What methods are needed to read & write an external text file?
I've noticed a board method that let you see the directory the bot is in, and there are a lot of "storage" methods, but I'm not sure how they're supposed to be used.
Posted: Mon Aug 27, 2007 1:18 pm
by GregM
To read and write your own file:
Code: Select all
import java.io.*;
...
void writeFile() {
FileWriter writer = new FileWriter(board.getAgentPath() + File.separator + fileName);
writer.write(stuff);
writer.flush();
writer.close();
}
...
void readFile() {
BufferedReader reader = new BufferedReader(new FileReader(board.getAgentPath() + File.separator + fileName));
String line;
while((line = reader.readLine()) != null) {
//do something with line
}
reader.close();
}
To use the Lux API storage functions, your code will look like this:
Code: Select all
void storeHitList() {
board.storagePut("hitList", "Cluster, Yakool, EvilPixie, dustin");
}
void retrieveHitList() {
hitList = board.storageGet("hitList", "the hitlist is empty"); //this last is the default value if Lux can't find any data called "hitList"
}
If you use these functions, Lux will take care of the details of maintaining your data across sessions and you don't have to mess around with file i/o.