Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate file IO in Java?

Tags:

java

file

io

How can I create a class MockFile mimicking java.io.File w.r.t. file read and write? I using everywhere my own methods instead of new FileInputStream(....) and new FileOutputStream(....), so this part is no problem (I always delegate to the appropriate stream). The non-trivila part is the implementation of my MockFileInputStream and MockFileOutputStream in more complicated cases.

There's is no problem, when I first write to a file and then read it, I can simply use a ByteArrayOutputStream and so on. That's simple, but with interleaving reads and writes it can't work. Any better idea than writing my own version of ByteArrayOutputStream?

like image 902
maaartinus Avatar asked Nov 25 '25 23:11

maaartinus


1 Answers

I would use a real file and a real FileInputStream and FileOutputStream. Otherwise you're just exercising test code: pretty pointless really.

like image 153
user207421 Avatar answered Nov 27 '25 13:11

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!