打印错误文本并随机获取结果
Printing the wrong text and getting results randomly
这是我的代码,其中 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);
}
}
因此,每当文件的最后一行发生更改时,屏幕上打印的文本应更改为 "there is a w" 或 "there is not a w",具体取决于最后一行中是否有 w。但是,我似乎随机得到结果。例如,当文件的最后一个字符串包含 "w," 时,有时我会在屏幕上打印 "there is not a w"。当字符串中没有 w 时,我也得到了 "there is a w"。
我做错了什么?
您的代码做了一些奇怪的事情:为什么您在 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];
}
另请注意,如果您不打算部署为 Java脚本,您可能希望使用 Java 的 file watch service 而不是每帧都读取文件.
这是我的代码,其中 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);
}
}
因此,每当文件的最后一行发生更改时,屏幕上打印的文本应更改为 "there is a w" 或 "there is not a w",具体取决于最后一行中是否有 w。但是,我似乎随机得到结果。例如,当文件的最后一个字符串包含 "w," 时,有时我会在屏幕上打印 "there is not a w"。当字符串中没有 w 时,我也得到了 "there is a w"。
我做错了什么?
您的代码做了一些奇怪的事情:为什么您在 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];
}
另请注意,如果您不打算部署为 Java脚本,您可能希望使用 Java 的 file watch service 而不是每帧都读取文件.