java - 传递的字符串不被视为字符串

标签 java string url

我有一个 Java 程序,它使用 Java 的 nio 文件监视程序来监视文件夹。当在该文件夹中创建某些内容时,它会获取该文件的名称,并使用 FileInputStream InputStreamReader 将内容设置为字符串。然后将该字符串传递给一个类,该类使用该字符串作为打印报告的参数。报表服务器返回错误,表示:

no protocol: java.io.InputStreamReader@dda25b?timezone=America/New_York&vgen=1377628109&cmd=get_pg&page=1&viewer=java2

它似乎不喜欢字符串的部分,因为Java将其视为某种命令而不是字符串,改变了它所说的内容。我确信有一个简单的解决方案,但我不确定如何表达它。字符串看起来像这样:

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();
    }
}

}

最佳答案

“inetargs = in.toString()”行是问题所在。看起来您认为会将文件的内容读入字符串,但它不会执行任何操作!您必须使用其 read() 方法来读取文件内容。

关于java - 传递的字符串不被视为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18473114/

相关文章:

java - 删除字符串中逗号前的字符

c++ - 在 Boost 中使用持续时间合并仅时间

ios - 使用 Objective-C 从 IOS 上的提醒中打开我的应用程序

java - Android 谷歌地图 URL 查询 - 离线时放置标记

java - 不同语言、不同字符的网页的URL结构

java - 当必须始终捕获异常时,Java 程序怎么会崩溃?

java - 使用 WinSCP 每天在特定时间运行 JAR 文件

Java - 使用多个 CPU 的非并行代码

java - 在 Jspresso 中创建一个过滤器 View ,其中包含不带比较运算符的日期选择器

python 正则表达式: expression to match number and letters