faqts : Computers : Programming : Languages : JavaScript : Applets/Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

23 of 29 people (79%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I write a local file in NN calling into Java?

May 15th, 2000 08:59
Martin Honnen,


Your code needs to be trusted to request privilege to access the local 
file system. See the entry on how to run trusted script in NN.

Here is a function that takes a filename and a string as an argument 
and writes that string as the file content:

function writeFile (fileName, content) {
  if (navigator.appName == 'Netscape' && navigator.javaEnabled) {
    netscape.security.PrivilegeManager.enablePrivilege
('UniversalFileWrite');
    var pw = new java.io.PrintWriter(new java.io.BufferedWriter(new 
java.io.FileWriter(fileName)));
    pw.print(content);
    pw.flush();
    pw.close();
  }
}

Example:
writeFile('C:\\test.txt', 
    'All for Kibology.\nKibology for all.\n' + 
     'Scriptology at JavaScript.FAQTs.com')