FileWriterãªãã¸ã§ã¯ãã§ã®ãã¡ã¤ã«ä¸æ¸ãã«ã¤ãã¦
ååãJavaScriptã§File Read/Writeããã£ã¦ã¿ããã§ãã£ãéããFileWriterãªãã¸ã§ã¯ãã使ããã¨ã§ãã¡ã¤ã«ã®æ¸ãè¾¼ã¿ã¯è¡ãã¾ãããæ¢åã®ãã¡ã¤ã«ã«å¯¾ãã¦æ¸ãæããè¡ãã¨ããã¾ãåããªããã¨ãããã®ã§è£è¶³ã
ãã¡ã¤ã«æ¸ãè¾¼ã¿
fs.root.getFile('log.txt', {create: true}, function(fileEntry) { // Create a FileWriter object for our FileEntry (log.txt). fileEntry.createWriter(function(fileWriter) { fileWriter.onwriteend = function(e) { console.log('Write completed.'); }; fileWriter.onerror = function(e) { console.log('Write failed: ' + e.toString()); }; // Create a new Blob and write it to log.txt. var bb = new Blob(['Lorem Ipsum']); fileWriter.write(bb); }, onError); }, onError);
Â
ããã§ä½¿ã£ã¦ããFileWriterãªãã¸ã§ã¯ãã®writeé¢æ°ã¯ããJavaScript API Documentationãã«ã¦
NOTE: write does not adjust the file length if you overwrite an existing file with shorter content. Use truncate with the new content length to remove old data at the end of the file, if desired.
ã¨ããéããæ¢åã®ãã¡ã¤ã«å 容ããçãå 容ã§æ¸ãè¾¼ã¿ããéã¯ã
æ¸ãè¾¼ãã æååã®æ«å°¾ä»¥éã«ãæ¢åæååã®å·®åãæ®ã£ã¦ãã¾ãããã§ãã
Â
ä¾
æ¸ãè¾¼ã¿åã®ãã¡ã¤ã«ã®å 容 : Hello World! æ¸ãè¾¼ãå 容 : Good Bye æ¸ãè¾¼ã¿å¾ã®ãã¡ã¤ã«ã®å 容 : Good Byerld!
ããããæã¯truncateã使ãã¨ãã¡ã¤ã«ãåé¤ããã«ããã¡ã¤ã«ã®ä¸èº«ãã¯ãªã¢ã§ãã¾ãã
fs.root.getFile('log.txt' {create: true}, function(fileEntry) { // Create a FileWriter object for our FileEntry (log.txt). fileEntry.createWriter(function(fileWriter) { fileWriter.onwriteend = function(e) { console.log('Truncate completed.'); }; fileWriter.onerror = function(e) { console.log('Truncate failed: ' + e.toString()); }; fileWriter.truncate(0); // å¼æ°ä»¥éã®ãã¡ã¤ã«ã®ä¸èº«ãã¯ãªã¢ }, onError); }, onError);
truncate -> write ã®é ã§å®è¡ããã°ããã¡ã¤ã«æ¸ãæããä¸æãè¡ãã¾ãã
次åã¯ããã¤ããçµã¿åããã¦ãèªåãã¹ãããã£ã¦ã¿ããï¼