我有一个 Java 程序正在使用 Java 的 nio 文件观察器监视文件夹。在该文件夹中创建某些内容时,它会获取该文件的名称并使用FileInputStream InputStreamReader
它将内容设置为字符串。然后将该字符串传递给一个类,该类使用该字符串作为打印报告的参数。报表服务器返回一个错误,说:
无协议:java.io.InputStreamReader@dda25b?timezone=America/New_York&vgen=1377628109&cmd=get_pg&page=1&viewer=java2
看起来它不喜欢 String 的部分,因为 Java 将它视为某种命令而不是 String,从而改变了它所说的内容。我确信有一个简单的解决方案,但我不确定如何表达它。字符串如下所示:
serverURL:port/?report=repo:reportname&datasource=datasource&prompt0=Date(2014,1,2)
代码:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
public class watching {
public static void main(String[] args) {
try {
String dirToWatch = "\\\\DIRECTORY\\PATH\\HERE\\";
WatchService watcher = FileSystems.getDefault().newWatchService();
Path logDir = Paths.get(dirToWatch);
logDir.register(watcher, ENTRY_CREATE);
while (true) {
WatchKey key = watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == ENTRY_CREATE) {
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context();
String thisfile = filename.toString();
//System.out.printf("%s was created in log dir.", filename.getFileName());
FileInputStream fis = new FileInputStream(dirToWatch+thisfile);
InputStreamReader in = new InputStreamReader(fis, "UTF-8");
String inetargs = in.toString();
inetprint printer = new inetprint (inetargs);
}
}
key.reset();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}