2009-12-12 94 views
2

如何使用JavaScript編寫和讀取文本文件?使用JavaScript讀取和寫入文本文件

+1

你需要提供更多上下文。在網頁瀏覽器中? (你不行。)在Windows shell中? (使用FileSystemObject。)在其他一些環境中? – 2009-12-12 09:22:50

回答

6

您需要在提供用於訪問文件系統的API的主機環境中運行JS。

如果你在Windows上,那麼you can use WSH to achieve this

在正常安全條件下運行瀏覽器的JS無法訪問文件系統。

0

您無法使用Java Script訪問您的文件系統,因此很遺憾,您不能使用

+0

在*網頁瀏覽器*。 JavaScript不限於網頁瀏覽器。 – 2009-12-12 09:15:17

+0

@Crowder:技術上你是對的,但我會認爲它*事實上*除非另有明確規定。我會假設一個.NET應用程序在Windows上運行,除非你告訴我它在Mono上運行。 – 2009-12-12 09:22:59

+3

@ dnl.vssll:永遠不要假設,它會使「屁股」脫離「你」和「我」。 – 2009-12-12 11:54:16

4

您不能。按照設計,瀏覽器中的JavaScript無法訪問用戶的文件系統。

6

技術例如,這可能會有幫助。

//Your text file location on system 
var savefile = "c:\\yourtextfile.txt"; 
try { 
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 

    var file = Components.classes["@mozilla.org/file/local;1"] 
    .createInstance(Components.interfaces.nsILocalFile); 
file.initWithPath(savefile); 
if (file.exists() == false) { 
    alert("Creating file... "); 
    file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420); 
} 

var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"] 
    .createInstance(Components.interfaces.nsIFileOutputStream); 

outputStream.init(file, 0x04 | 0x08 | 0x20, 420, 0); 
var output = "Your text here"; 
var result = outputStream.write(output, output.length); 
outputStream.close(); 

alert("Done"); 
} 
catch (e) { 
    alert("Some error occured"); 
} 

它的工作對我來說,希望對你的作品,以及:)

+0

適合我。謝謝。 Chrome是否具備此功能? – 2012-04-09 14:10:04

2

有一個有趣的劇本,如果你願意使用的Greasemonkey:

// ==UserScript== 
// @name   Store notes for every website 
// @creator  Xavi Esteve 
// @namespace  http://www.xaviesteve.com 
// @description Shows a little notebook at the right bottom of every page that stores any text you type in automatically. Each domain has his separate notebook which can be shown/hidden with a click. 
// @version  1.3 
// @include  * 
// @exclude  http*://*.google.*/mail/* 
// @exclude  http*://*.googlemail.* 
// @exclude  file:///* 
// ==/UserScript== 

if (self == top) { 

// VARIABLES 
var e = document.domain.split(/\./); 
gdomain = document.domain; 
var gotit = GM_getValue(gdomain, '[Type notes for '+gdomain+']'); 
// FUNCTIONS 
function saveit() { 
    GM_setValue(gdomain, document.getElementById('gm_textarea').value); 
    return false; 
} 
/* Insert HTML */ 
/* div */ 
var div = document.createElement('div'); 
div.innerHTML = '<a onmousedown="var tbh = document.getElementById(\'gm_tobehiden\');if(tbh.style.display==\'none\'){tbh.style.display=\'block\';document.getElementById(\'gm_textarea\').focus();}else{tbh.style.display = \'none\';}return false;" title="Notebook">'+gdomain+'</a><div id="gm_tobehiden"><div id="gm_title"></div></div>'; 
div.id = "gm_notebook"; 
document.body.insertBefore(div, document.body.lastChild); 
/* textarea */ 
var textarea = document.createElement('textarea'); 
textarea.appendChild(document.createTextNode(gotit)); 
textarea.addEventListener('keyup', saveit, false); 
textarea.addEventListener('click', saveit, false); 
textarea.id = "gm_textarea"; 
var gm_title = document.getElementById('gm_title'); 
gm_title.parentNode.insertBefore(textarea, gm_title.nextSibling); 
/* Insert CSS */ 
    var menuCode = new Array(); 
    menuCode.push("#gm_notebook {-moz-opacity:0.9;position:fixed;bottom:40px;right:5px;border:1px solid #ccc;font-size:10px;color:#333;background:#f1f1f1;padding:3px 5px 5px 5px;font-family:Arial,sans-serif}#gm_notebook a {color:#0085d5;margin:2px;cursor:pointer}"); 
    menuCode.push("#gm_tobehiden {display:none;width:200px;height:300px;padding:5px}"); // Change display to block to show the notebook by default 
    menuCode.push("#gm_textarea {width:100%;height:100%;color:#000;font-family:monospace}"); 
    var style = document.createElement('style'); 
    style.type = 'text/css'; 
    style.innerHTML = menuCode.join(''); 
    menuCode.length = 0; 

    try { document.getElementsByTagName('head')[0].appendChild(style); } 
    catch(e) {} 

}