贴火狐IO的一段代码
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test Page</title>
<script type='text/javascript' src='/TestJsIO/dwr/interface/testclass.js'></script>
<script type='text/javascript' src='/TestJsIO/dwr/engine.js'></script>
<SCRIPT type="text/javascript">
function test(){
testclass.getString(function(data){
ioFile(data);
});
}
function ioFile(str){
//alert("尝试创建automation对象...");
try{
var fso=new ActiveXObject("Scripting.FileSystemObject");
var newFileObject=fso.CreateTextFile("e:\\ie.js", true);
newFileObject.WriteLine(str);
newFileObject.close();
}catch(e){
try{
saveFile(str);
}catch(e){
alert("请使用IE或者firefox!");
}
}
}
function saveFile(str){
var path="e:\\firefox.js";
var content=str+"\n this is from ff...";
try {
alert("尝试解除安全限制...");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("安全限制解除异常...");
}
//初始化文件接口,实现nsILocalFile接口
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
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 converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset = 'UTF-8';
var convSource = converter.ConvertFromUnicode(content);
var result = outputStream.write( convSource, convSource.length );
outputStream.close();
}
</SCRIPT>
</head>
<body>
<button onclick="test()">点击进行测试</button>
</body>
</html>
以下是官方的参考...
http://developer.mozilla.org/en/docs/nsIFile
http://developer.mozilla.org/en/docs/Reading_textual_data
以下是转码...