Reading & writing an external text file?

AI discussion, ideas, and SDK help.
Post Reply
User avatar
SunTzu
Lux Cartographer
Posts: 1586
Joined: Sat Jan 14, 2006 1:48 am
Location: Maryland

Reading & writing an external text file?

Post by SunTzu » Mon Aug 27, 2007 11:23 am

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.

User avatar
GregM
Luxer
Posts: 252
Joined: Wed Jun 01, 2005 4:33 pm

Post by GregM » Mon Aug 27, 2007 1:18 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 59 guests