How to read and write to files?

Talk about add-ons and extension development.
Noitidart
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Re: How to read and write to files?

Post by Noitidart »

lithopsian wrote:Safe file output creates a temporary file with your writes, then replaces the original file with it when you close (finish?) it. It makes sense that it wouldn't work with append although I haven't tested it or looked at the source to check. Some of the other flags will still work.

ah i think it makes more sense now.

so is there a need for me to call closeSafeFile or closeFileoutputstream when using these functions?
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: How to read and write to files?

Post by lithopsian »

When you have finished writing a safe file output stream, you should call finish() to cause the old file to be overwritten with the new one. Then you should really call close() to close the stream although your write will succeed even without it. The FileUtils helper closeSafeFileOutputStream() method does both of these for you without you needing to worry about whether the stream is safe or not. The finish() call can fail for a variety of reasons and the helper will take care of this for you, unfortunately without telling you very much about it.
Noitidart
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Re: How to read and write to files?

Post by Noitidart »

Ahhh thats super to know thanks lith I get so much info that should be on mdn. Ill try to update all.

Also the fileUtils non-safe, there doesnt look like there is closeFileStream, so ill have to call finish and close myself huh?
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: How to read and write to files?

Post by lithopsian »

You can call the same closeSafeFileOutputStream(). It only calls finish() if it is a safe stream.
Noitidart
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Re: How to read and write to files?

Post by Noitidart »

Ah ok thanks lith!
yajd
Posts: 55
Joined: February 3rd, 2014, 6:02 pm

Re: How to read and write to files?

Post by yajd »

Guys we're doing this all wrong. We need to use OSFile.jsm this thing is spectacular. Superbly async. https://developer.mozilla.org/en-US/doc ... ain_thread

Great place to learn tasks and promises too!
poleta33
Posts: 120
Joined: October 14th, 2004, 2:06 pm

Re: How to read and write to files?

Post by poleta33 »

seems this readFile function do not work with WEISO-8859-15 encoded files...
Noitidart
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Re: How to read and write to files?

Post by Noitidart »

poleta33 wrote:seems this readFile function do not work with WEISO-8859-15 encoded files...
Try OS.File.read with proper "encoding" key in opts argument. https://developer.mozilla.org/en-US/doc ... hread#read()
Post Reply