public FileOutputStream createRobotFileStream(String fileName, boolean append)
      throws IOException {
    final Thread c = Thread.currentThread();

    final IHostedThread robotProxy = getRobotProxy(c);

    if (robotProxy == null) {
      syserr.println("RobotProxy is null");
      return null;
    }
    if (!robotProxy.getStatics().isAdvancedRobot()) {
      throw new RobotException("Only advanced robots could create files");
    }

    final File dir = robotProxy.getRobotFileSystemManager().getWritableDirectory();

    if (!dir.exists()) {
      robotProxy.println("SYSTEM: Creating a data directory for you.");
      AccessController.doPrivileged(
          new PrivilegedAction<Object>() {
            public Object run() {
              outputStreamThreads.add(c);
              if (!dir.exists() && !dir.mkdirs()) {
                syserr.println("Can't create dir " + dir.toString());
              }
              return null;
            }
          });
    }

    final RobotFileSystemManager fileSystemManager = robotProxy.getRobotFileSystemManager();

    File f = new File(fileName);
    long len;

    if (f.exists()) {
      len = f.length();
    } else {
      fileSystemManager.checkQuota();
      len = 0;
    }

    if (!append) {
      fileSystemManager.adjustQuota(-len);
    }

    outputStreamThreads.add(c);
    return new RobotFileOutputStream(fileName, append, fileSystemManager);
  }