When FileInputStream is first created, I understand that it takes in the data that the file has. Now let's say I modify the file with FileOutputStream and I haven't closed FileInputStream, will it change the data that the FileInputStream has inside or will it remain the same? If it remains the same, is there a way to just add the new changes to the FileInputStream instead of reading the entire file again?
-
2"When FileInputStream is first created, I understand that it takes in the data that the file has. " ... It reads the file when you "ask" it to read it. That is the purpose of a stream.Tom– Tom2015-08-06 14:14:32 +00:00Commented Aug 6, 2015 at 14:14
-
2Also, if the inputstream read the file, it also lock the file and you cannot work on it until you close the stream.Jkike– Jkike2015-08-06 14:17:58 +00:00Commented Aug 6, 2015 at 14:17
-
I think I understand now, thanks for the info!silverAndroid– silverAndroid2015-08-06 14:20:02 +00:00Commented Aug 6, 2015 at 14:20
Add a comment
|
1 Answer
FileInputStream does not take the data from the file. Data is taken on demand when you call read().
If you try to modify the file with FileOutputStream and I haven't closed FileInputStream on Windows this will fail with Exception. On Unix/Linux system you will read part of the old and part of the new file (which was overwritten by FileOutputStream). If you really need to read and write in same file at the same time, it will be better to use RandomAccessFile