Android: open file share every time I want to register

I make my own log in android by writing a file. I am currently calling a function that opens a file and adds a line to it. However, I read somewhere that it is successful (the operation is hard) to open the file every time I enter it. It's true? What is the alternative here? Should I open the file, leave it open for the duration of the application and just add to it? Or what do you suggest?

Thank you very much

+4
source share
1 answer

It depends on how many magazines you make. How many logs takes a second, about how much data, how important logs are, etc. If you just added something to the file a couple of times a minute, if this does not happen on the main thread and slows down other things, you are probably fine. If you need to implement something more efficient, you may need simple caching.

You can save the log data cache in memory and save it in a file when it reaches a certain size (every 100 lines) or when a certain amount of time passes (every 5 minutes).

If you take this approach, be sure to override onPause and onDestroy and remember to write the cached log data to a file before exiting the application if the user does not return.

+1
source

Source: https://habr.com/ru/post/1432386/


All Articles