-
Streaming Files
Hi all... I've been considering trying to a third-party program for a game I play... but I can't figure out how to read from a file. The game writes chat logs, but how would I read from them as they are written to...
e.g
Code:
start game
start third-party program (my program) [tpp]
tpp reads from chat logs
game writes to chat logs
tpp reads only the new data from chat logs (HOW DO I DO THIS!)
I'm not sure whether its possible, but I think it is, I just dont know how. Could be useful for other things too, but this is where I'm at. Sorry, dont have any code, because I hit this logic block while planning it out.
Thanks for any help in advance,
Singing Boyo
-
If you have control of the stream, then you could just use a pipe stream. Otherwise, I guess you could continously read/mark/reset the position on a separate thread.
-
read/mark/reset? any chance I could get an example? I dont have any way to get control of the stream as far as I know.
Hmm... one thing, what if I used a BufferedReader on a FileInputStream, and continuously called readLine() (or whatever it is, been using scanner lately) on it. In my opinion, that would ensure that I got the new lines as the came through.
Thanks for the help,
SB
-
this seems to work, but needs work on performance. Edit the test file manually, save, and see the console gets updated as well.
Code:
import java.io.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException{
InputStream in = new FileInputStream("foo.bar");
int c;
while(true){
try{
c = in.read();
if(c==-1) continue;
else System.out.print((char)c);
}catch(IOException ex){}
}
}
}
-
hmmmm seems that if I del some lines of text, it would only update lines after the current mark...
-
Well, the chat logs aren't having deleted content, so deleted lines shouldn't be a problem.
Thanks for the code, haven't tested it yet, but it looks like it'll work.