Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用JavaScript读写文本文件_Javascript_Text Files - Fatal编程技术网

用JavaScript读写文本文件

用JavaScript读写文本文件,javascript,text-files,Javascript,Text Files,如何使用JavaScript编写和读取文本文件?您不能使用Java脚本访问您的文件系统,因此很遗憾,您不能使用Java脚本。根据设计,浏览器中的JavaScript无法访问用户的文件系统。您需要在提供用于访问文件系统的API的主机环境中运行JS 如果你在Windows上,那么 在正常的安全条件下,运行浏览器的JS无法访问文件系统。在FF 3.6中,这是可能的,请参阅我在上的技术示例。如果您使用的是Firefox,这可能会有所帮助 //Your text file location on syst

如何使用JavaScript编写和读取文本文件?

您不能使用Java脚本访问您的文件系统,因此很遗憾,您不能使用Java脚本。根据设计,浏览器中的JavaScript无法访问用户的文件系统。

您需要在提供用于访问文件系统的API的主机环境中运行JS

如果你在Windows上,那么


在正常的安全条件下,运行浏览器的JS无法访问文件系统。

在FF 3.6中,这是可能的,请参阅我在

上的技术示例。如果您使用的是Firefox,这可能会有所帮助

//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");
}

它对我有效,希望对你也有效:)

如果你愿意使用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) {}

}
/==UserScript==
//@name每个网站的商店说明
//@创造者哈维·埃斯特夫
//@名称空间http://www.xaviesteve.com
//@description在每页的右下角显示一个小笔记本,可以自动存储您键入的任何文本。每个域都有其单独的笔记本,只需单击即可显示/隐藏。
//@version 1.3
//@包括*
//@exclude http*://*.google.*/mail/*
//@exclude http*://*。谷歌邮箱*
//@exclude文件:///*
//==/UserScript==
if(self==顶部){
//变数
var e=document.domain.split(/\./);
gdomain=document.domain;
var gotit=GM_getValue(gdomain,“[为“+gdomain+]”键入注释];
//功能
函数saveit(){
GM_setValue(gdomain,document.getElementById('GM_textarea').value);
返回false;
}
/*插入HTML*/
/*div*/
var div=document.createElement('div');
div.innerHTML=''+gdomain+'';
div.id=“gm_笔记本”;
document.body.insertBefore(div,document.body.lastChild);
/*文本区*/
var textarea=document.createElement('textarea');
appendChild(document.createTextNode(gott));
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);
/*插入CSS*/
var menuCode=新数组();
menuCode.push(#gm#U笔记本{-moz不透明度:0.9;位置:固定;底部:40px;右:5px;边框:1px实心#ccc;字体大小:10px;颜色:333;背景:f1f1f1;填充:3px 5px 5px 5px;字体系列:Arial,无衬线}gm#U笔记本a{颜色:0085d5;边框:2px;光标:指针});
menuCode.push(“#gm_tobehiden{display:none;width:200px;height:300px;padding:5px}”);//将display更改为block以默认显示笔记本
menuCode.push(“#gm#u 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;
请尝试{document.getElementsByTagName('head')[0].appendChild(样式);}
捕获(e){}
}

在web浏览器中。JavaScript并不局限于web浏览器。@Crowder:从技术上讲,你是对的,但我认为这是事实,除非另有明确规定。我会假设一个.NET应用程序在Windows上运行,除非你告诉我它在Mono上运行。@dnl.vssll:永远不要假设,它会把“u”和“我”变成“屁股”。为了进一步说明我的评论,JavaScript在比web更多的地方被广泛使用,桌面小部件和shell脚本只是一些例子。平心而论,提问者的问题不够具体是他的错。你需要提供更多的背景。在网络浏览器中?(你不能)在Windows外壳中?(使用FileSystemObject。)在其他环境中?适合我。谢谢Chrome有这个功能吗?