Read and Write Files

Develop

  1. Create a new project and name it files_read_write.
  2. Select the ScriptingServices sub-folder of the project and open the pop-up menu.
  3. Choose New -> Scripting Service.
  4. Choose Server-Side JavaScript Service from the list of available templates.
  5. Give it a meaningful name (e.g files_read_write.js).
  6. Replace the generated code in files_read_write.js with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
	/* globals $ */
	/* eslint-env node, dirigible */
	
	var files = require('io/files');
	var response = require('net/http/response');
	
	files.createFile("../temp/test1/test5.txt");
	var file = files.get("../temp/test1/test2.txt");
	response.println("[File Exists?]: " + file.exists());
	response.println("[File Is File?]: " + file.isFile());
	
	var content = files.readText("../temp/test1/test5.txt");
	response.println("[File Content]: " + content);
	
	files.writeText("../temp/test1/test5.txt", "Some content");
	response.println("[File Write]: " + content);
	
	content = files.readText("../temp/test1/test5.txt");
	response.println("[File Content]: " + content);
	
	var bytes = files.read("../temp/test1/test5.txt");
	response.println("[File Content as Bytes]: " + bytes);
	
	bytes = [83, 84, 85];
	files.write("../temp/test1/test5.txt", bytes);
	
	content = files.readText("../temp/test1/test5.txt");
	response.println("[File Content]: " + content);
	
	response.flush();
	response.close();

Discover

To discover all available services, you can go to the Registry.

  1. From the main menu, choose Window -> Show Perspective -> Registry.
  2. The Registry perspective represents a view to the enabled runtime content. From its menu, choose Discover -> JavaScript to open the currently available server-side JavaScript service endpoints.
  3. You can see the list of available endpoints, where you can find yours by naming convention: {project}/{service path}
Edit