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?

22 of 26 people (85%) answered Yes
Recently 6 of 8 people (75%) answered Yes

Entry

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

May 15th, 2000 08:46
Martin Honnen,


Your code needs to be trusted to request privilege to read files from 
the local file system. Trust is given to local files (i.e. script 
loaded via file: urls) or when signed or when the browser accepts 
codebase principals to do so. See the entry on how to run trusted 
scripts in NN:

The function reads in the content of a text file into a JavaScript 
string:

function readFile (fileName) {
  if (navigator.appName == 'Netscape' && navigator.javaEnabled) {
    netscape.security.PrivilegeManager.enablePrivilege
('UniversalFileRead');
    var br = new java.io.BufferedReader(new java.io.FileReader
(fileName));
    var line;
    var result = '';
    while ((line = br.readLine())) 
      result += line + java.lang.System.getProperty('line.separator');
    return result;
  }
}

Example:
  alert(readFile('C:\\autoexec.bat'))