JavaScript로 텍스트 파일을 읽고 쓰려면 어떻게해야합니까?JavaScript로 텍스트 파일 읽기 및 쓰기
답변
파일 시스템에 액세스하기위한 API를 제공하는 호스트 환경에서 JS를 실행해야합니다.
Windows 사용자는 you can use WSH to achieve this입니다.
브라우저를 실행중인 JS는 정상적인 보안 상태에서 파일 시스템에 액세스 할 수 없습니다. 당신이 파이어 폭스를 사용하는 경우는 가능하다 FF 3.6에서
이당신은 자바 스크립트를 사용하여 파일 시스템을 액세스 할 수 없습니다, 그래서 불행하게도 당신이 할 수는 없습니다
* 웹 브라우저 *. 자바 스크립트는 웹 브라우저에만 국한되지 않습니다. –
@Crowder : 기술적으로 당신 말이 맞습니다. 그러나 명시 적으로 달리 지정되지 않는 한 사실상 *로 간주 할 것입니다. Mono에서 실행 중이라고 말하지 않으면 .NET 응용 프로그램이 Windows에서 실행되고 있다고 가정합니다. –
@ dnl.vssll : 절대로 "u"와 "me"에서 "엉덩이"가 나오지 않는다고 가정하십시오. –
당신은 할 수 없습니다. 브라우저의 JavaScript는 의도적으로 사용자의 파일 시스템에 액세스하지 못합니다.
에서 내 기술 예를 보려면이 도움이 될 수 있습니다.
//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");
}
그것은 나를 위해 일한
, 당신을위한 희망의 작품뿐만 아니라 :
나를 위해 작동합니다. 감사. 크롬에이 기능이 있습니까? –
흥미로운 스크립트가 경우에 당신은 그리스 몽키를 사용하고자하는, 거기 :
// ==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) {}
}
당신은해야합니다 더 많은 맥락을 제공하라. 웹 브라우저에서? (할 수 없습니다.) Windows 쉘에서? (FileSystemObject를 사용하십시오.) 다른 환경에서는? –