打印错误的文本并随机获取结果

时间:2015-09-15 19:57:56

标签: processing

这是我的代码,其中lastLine检查文件的最后一个:

import java.util.Random;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

    public String lastLine( File file ) {
    RandomAccessFile fileHandler = null;
    try {
        fileHandler = new RandomAccessFile( file, "r" );
        long fileLength = fileHandler.length() - 1;
        StringBuilder sb = new StringBuilder();

        for(long filePointer = fileLength; filePointer != -1; filePointer--){
            fileHandler.seek( filePointer );
            int readByte = fileHandler.readByte();

            if( readByte == 0xA ) {
                if( filePointer == fileLength ) {
                    continue;
                }
                break;

            } else if( readByte == 0xD ) {
                if( filePointer == fileLength - 1 ) {
                    continue;
                }
                break;
            }

            sb.append( ( char ) readByte );
        }

        String lastLine = sb.reverse().toString();
        return lastLine;
    } catch( java.io.FileNotFoundException e ) {
        e.printStackTrace();
        return null;
    } catch( java.io.IOException e ) {
        e.printStackTrace();
        return null;
    } finally {
        if (fileHandler != null )
            try {
                fileHandler.close();
            } catch (IOException e) {
                /* ignore */
            }
    }
}
    File file = new File("lines.txt");
    String last = lastLine(file);
    String previous = null;

float r = random(0,255);
float g = random(0, 255);
float b = random(0, 255);



    public void settings() {
        size(500, 500);
    }
    public void setup() {
      frameRate(60);
        stroke(155, 0, 0);
        textAlign(CENTER, CENTER);
        textSize(30);
                background(r+(random(0,100)), g+(random(0,100)), b+(random(0,100)));
    }

    void draw() {

      previous = lastLine(file);

      if (last.equals(previous)) {
        last = lastLine(file);
      }

      if (!last.equals(previous)) {
          if (last.indexOf("w") != -1) {
            text("there is a w", 255, 255);
            fill(r, g, b);
          }
          else {
            text("there is not a w", 255, 255);
          }
        fill(50);
        last = lastLine(file);
      }
    }

因此,每当文件的最后一行被更改时,根据最后一行中是否有w,屏幕上打印的文本应更改为“有w”或“没有w”。但是,我似乎随机得到了结果。例如,当文件的最后一个字符串包含“w”时,有时我会在屏幕上打印“没有w”。当字符串中没有w时,我也得到了“有一个w”。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

你的代码做了一些奇怪的事情:为什么你在draw()函数中分别读取文件3次?为什么一次读取一个字节的文件?

这是一个似乎可以做你想要的例子,而不是试图通过你的代码:

String previous;

void draw() {
  String current = lastLine();
  if (!current.equals(previous)) {
    background(random(255), random(255), random(255));

    if (current.contains("w")) {
      text("YES W", 0, 50);
    } else {
      text("NO W", 0, 50);
    }
  }

  previous = current;
}

public String lastLine() {
  String lines[] = loadStrings("list.txt");

  if(lines.length == 0){
    return "";
  }

  return lines[lines.length-1];
}

另请注意,如果您不打算将其部署为JavaScript,则可能需要使用Java file watch service而不是每帧读取文件。