broguesquadron
broguesquadron

Reputation: 35

Passed String not being treated as String

I have a Java program that is watching a folder using Java's nio file watcher. When something is created in that folder, it gets the name of that file and using FileInputStream InputStreamReader it sets the contents to a String. The String is then passed to a class that uses this String as the parameters for printing a report. The report server is returning an error, saying:

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

It would seem that it doesn't like the part of the String because Java is treating it as some kind of command instead of a String, changing what it says. I'm sure there is a straightforward solution, but I am unsure of how to phrase it. The String looks like this:

serverURL:port/?report=repo:reportname&datasource=datasource&prompt0=Date(2014,1,2)

CODE:

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

}

Upvotes: 0

Views: 148

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

The line "inetargs = in.toString()" is the problem. It looks like you think that will read the contents of the file into a string, but it will do nothing of the sort! You must use its read() methods to read the file contents.

Upvotes: 3

Related Questions