vendredi 29 novembre 2019

How to continuously read an online/web file in java?

I am making a simple text-only instant messenger in java. The way that it currently works is that all the messages are put into an online text file by sending a php file a request (I know this is bad for security, but this is just to learn how to do web-connected apps in java.) Currently, I am continually fetching the entire contents of the messages.txt file and placing them into my JTextPane like this:

while(true) {
URL url = new URL("{path to text file}");
InputStream in = url.openStream();
Scanner s = new Scanner(in).useDelimiter("\\A");
String conversation = s.hasNext() ? s.next() : "";
textPane.setText(conversation);
}

But when the conversation becomes long enough, it lags as it is fetching 100kb+ files constantly from a web server.

What I want to happen is to only read the changes to the file so that it doesn't lag and max out my internet connection by requesting enourmous amounts of plain text files. I don't want to just make it run every 2 seconds because it's an instant messenger, no delays.

How would I go around only fetching the changes to the file and adding them to the text pane?




Aucun commentaire:

Enregistrer un commentaire