MozillaZine

How to read and write to files?

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

Post Posted February 9th, 2014, 4:57 pm

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

Post Posted February 9th, 2014, 5:12 pm

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

Post Posted February 10th, 2014, 8:44 am

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

Post Posted February 10th, 2014, 10:41 am

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

Post Posted February 10th, 2014, 10:28 pm

Ah ok thanks lith!

yajd
 
Posts: 55
Joined: February 3rd, 2014, 6:02 pm

Post Posted February 25th, 2014, 3:21 pm

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: 115
Joined: October 14th, 2004, 2:06 pm

Post Posted September 30th, 2016, 1:56 am

seems this readFile function do not work with WEISO-8859-15 encoded files...

Noitidart
 
Posts: 1168
Joined: September 16th, 2007, 8:01 am

Post Posted October 6th, 2016, 12:16 pm

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 ... read#read()

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 5 guests