Ceyhan ILERI
Ceyhan ILERI

Reputation: 57

Reading InputStream periodically

I have an input stream in my AsyncTask -which is coming from an FTP server with retrieveFileStream method and contains just one line- and i want to read the same file periodically, like every 30 seconds. I can read the file just once but i don't know how to do that periodically. Here is the reading once part of my code :

ftpClient.connect("f11-preview.125mb.com", 21);
System.out.println(ftpClient.getReplyString());
ftpClient.enterLocalPassiveMode();
System.out.println(ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.USER, "xxx");
System.out.println(ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.PASS, "yyy");
System.out.println(ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.CWD, "/CodeJava");
System.out.println(ftpClient.getReplyString());

// that part of the code i want to do periodically :
InputStream is= (ftpClient.retrieveFileStream("deneme1.txt"));
System.out.println("Input Stream has opened.");
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
theString = writer.toString();
System.out.println(theString);

Upvotes: 0

Views: 247

Answers (1)

Roan
Roan

Reputation: 1206

You could use a timer:

new Timer().scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run() {
            //Your task here
        }
    }, 0, 30 * 1000);

You could also use a SchelduledExcutorService as pointed out by Tony.
You can read about those here: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
or
here: https://gist.github.com/codingtony/c21e278b3111d5256f0d



Hope this helps.

Upvotes: 2

Related Questions