• Learning Rhino 2


    Hello Rhino
    First, look at my examples' file structure:

    RhinoTest
    	lib
    		js.jar
    	scripts
    		hello.js
    		file1.js
    	run.bat
    

    My first rhino example is hello.js:

    for (var i = 0; i < arguments.length; i++) {
        print('arguments[' + i + '] = ' + arguments[i]);
    }
    
    function add() {
        var result = 0;
        for (var i = 0; i < arguments.length; i++) {
            result += arguments[i];
        }
        return result;
    }
    print('2 + 5 + 3 = ' + add(2, 5, 3));
    

    The hello.js is executed through batch file run.bat:

    @echo off
    
    echo.
    echo ---------- hello ----------
    java -cp libjs.jar org.mozilla.javascript.tools.shell.Main scriptshello.js Hello Rhino
    

    And the result:

    arguments[0] = Hello
    arguments[1] = Rhino
    2 + 5 + 3 = 10
    

    How to get user’s current working directory
    We all know java.lang.System class contains several useful fields and methods.
    The getProperties method can be used to determine the current system properties.

    var System = Packages.java.lang.System;
    
    print('Working directory = ' + System.getProperty('user.dir'));
    print('Home directory = ' + System.getProperty('user.home'));
    print('Account name = ' + System.getProperty('user.name'));
    print('Line separator ("\n" on UNIX) = ' + (String(System.getProperty('line.separator')) === 'rn' ? '\r\n' : '\n'));
    print('File separator ("/" on UNIX) = ' + System.getProperty('file.separator'));
    

    Output:

    Working directory = D:documentsRhinoTest
    Home directory = C:Documents and Settingssanshi
    Account name = sanshi
    Line separator ("n" on UNIX) = rn
    File separator ("/" on UNIX) = 
    

    File or Path exist

    var System = Packages.java.lang.System;
    var File = Packages.java.io.File;
    var userDir = System.getProperty('user.dir');
    
    var file = new File(userDir + "\scripts\hello.js");
    print(file.exists());   /* true */
    print(new File(userDir + "\scripts").exists());   /* true */
    

    Read File to string
    Use LineNumberReader to read file to string:

    var System = Packages.java.lang.System;
    var File = Packages.java.io.File;
    var lineSeparator = System.getProperty('line.separator');
    var userDir = System.getProperty('user.dir');
    
    var reader = new Packages.java.io.LineNumberReader(
        new Packages.java.io.FileReader(userDir + "\scripts\hello.js"));
    var lines = [];
    while (line = reader.readLine()) {
        lines.push(line);
        lines.push(lineSeparator);
    }
    reader.close();
    print(lines.join(''));
    

    Write string to file
    Refer to BufferedWriter

    var System = Packages.java.lang.System;
    var userDir = System.getProperty('user.dir');
    
    var writer = new Packages.java.io.PrintWriter(
        new Packages.java.io.BufferedWriter(
    	    new Packages.java.io.FileWriter(userDir + "\scripts\output.txt")));
    writer.write("This is file content.rnIt's easy!");
    writer.flush();
    writer.close();
    

    Refer to JsDoc Toolkit

  • 相关阅读:
    scanf和cin的返回值
    C++ STL中Map的按Key排序跟按Value排序
    name lookup of 'res' changed for new ISO 'res' scoping
    string中c_str()、data()、copy(p,n)函数的用法
    C中malloc的使用(转)
    codeforces 653B B. Bear and Compressing(dfs)
    codeforces 653A A. Bear and Three Balls(水题)
    hdu-5646 DZY Loves Partition(贪心)
    hdu-5645 DZY Loves Balls(水题)
    codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)
  • 原文地址:https://www.cnblogs.com/sanshi/p/1490156.html
Copyright © 2020-2023  润新知