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; iThe hello.js is executed through batch file run.bat:
@echo offecho.echo ---------- hello ----------java -cp libjs.jar org.mozilla.javascript.tools.shell.Main scriptshello.js Hello RhinoAnd the result:
arguments[0] = Helloarguments[1] = Rhino2 + 5 + 3 = 10How 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' ? 'rn' : 'n'));print('File separator ("/" on UNIX) = ' + System.getProperty('file.separator'));Output:
Working directory = D:documentsRhinoTestHome directory = C:Documents and SettingssanshiAccount name = sanshiLine separator ("n" on UNIX) = rnFile 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 + "scriptshello.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 + "scriptshello.js"));var lines = [];while (line = reader.readLine()) { lines.push(line); lines.push(lineSeparator);}reader.close();print(lines.join(''));Write string to file
Refer to BufferedWritervar 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 + "scriptsoutput.txt")));writer.write("This is file content.rnIt's easy!");writer.flush();writer.close();Refer to JsDoc Toolkit
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2471/viewspace-2806217/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Learning Rhino - 1
- Reinforcement Learning Chapter2APT
- learning koa2.x
- JS引擎(2):Java平臺上JavaScript引擎—Rhino/Nashorn概述JSJavaScript
- Rhino 8 中文安裝包「犀牛 Rhino 8破解新功能」
- 【Learning eBPF-2】eBPF 的“Hello world”eBPF
- Acwing Arithmetic Learning:資料結構(2)資料結構
- 開發Rhino外掛
- 翻譯 | Learning React Without Using React Part 2React
- rhino5.0安裝教程
- rhino 的擠出曲線命令
- 論文解讀(node2vec)《node2vec Scalable Feature Learning for Networks》
- Rhino基礎操作3 - 出圖篇
- 中文Rhino 8新增內容介紹
- 犀牛3D建模Rhino 7 Mac3DMac
- Rhino 7犀牛3D建模工具3D
- [Active Learning] Multi-Criteria-based Active Learning
- Imitation LearningMIT
- learning sequelize
- Meta Learning
- Learning TypeScriptTypeScript
- 《Learning ELK Stack》2 構建第一條ELK資料管道
- Rhino 7 犀牛3D建模軟體3D
- Rhino 7 犀牛3D建模軟體3D
- 論文解讀(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
- gitglossary learning by examplesGit
- 《machine learning》引言Mac
- Machine Learning with SklearnMac
- Python Learning: 03Python
- Python Learning: 01Python
- Learning MySQL and MariaDBMySql
- 犀牛3D建模軟體Rhino 7 for Mac3DMac
- Rhino 7 for Mac(犀牛3D建模軟體)Mac3D
- Rhino 7 for Mac 犀牛3D建模軟體Mac3D
- Rhino 7 for Mac(犀牛3D造型軟體)Mac3D
- 《深度學習》PDF Deep Learning: Adaptive Computation and Machine Learning series深度學習APTMac
- [論文閱讀筆記] metapath2vec: Scalable Representation Learning for Heterogeneous Networks筆記
- 論文解讀(MLDG)《Learning to Generalize: Meta-Learning for Domain Generalization》AI