Class: File

DKTools.IO.File

File class

new DKTools.IO.File ()

Extends

Members

_extension String privatereadonly inherited overrides

_initialExtension String privatereadonly inherited

_name String privatereadonly inherited

_path String privatereadonly inherited

extension inherited

Gets extension of the entity
Since:
  • 3.0.0

initialExtension inherited

Gets initial extension of the entity
Since:
  • 5.0.0

name inherited

Gets name of the entity

path inherited

Gets path of the entity
Since:
  • 3.0.0

Methods

__processError (error, onError) private inherited

Name Type Description
error * Error
onError function optional Callback function upon completion of an operation with error
Since:
  • 6.1.0

_detectExtension () private

Detects the extension
Version:
  • 10.0.0
Since:
  • 5.0.0

copy (destination, object)Number

Copies file Returns a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_OPTIONS_ARE_NOT_AVAILABLE
Name Type Description
destination String Destination filename of the copy operation
object Object Options of an operation
Name Type Description
sync Boolean optional Use synchronous version of FileSystem.copyFile
createDirectory Boolean | Object optional Create a directory for the file
flags Number optional Modifiers for copy operation
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
createDirectory.options Object optional Options for FileSystem.mkdir or FileSystem.mkdirSync
Name Type Description
recursive Boolean optional Parent folders should be created
mode Number | String optional Directory permission
Since:
  • 7.0.0
Returns:
Type Description
Number Code of the result of an operation

copyAsync (destination, object)Promise.<Number> async

Copies file Asynchronous version of DKTools.IO.File.prototype.copy Promise resolves a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Name Type Default Description
destination String Destination filename of the copy operation
object Object {} optional Options of an operation
Name Type Description
createDirectory Boolean | Object optional Create a directory for the file
flags Number optional Modifiers for copy operation
createDirectory.options Object optional Options for FileSystem.mkdir or FileSystem.mkdirSync
Name Type Description
recursive Boolean optional Parent folders should be created
mode Number | String optional Directory permission
Since:
  • 7.0.0
Returns:
Type Description
Promise.<Number> Code of the result of an operation

equals (entity)Boolean inherited

Returns true is the entity is same
Name Type Description
entity DKTools.IO.Entity Entity
Since:
  • 11.2.1
Returns:
Type Description
Boolean Entity is same

exists ()Boolean inherited

Returns true if the entity exists
Version:
  • 3.0.0
Returns:
Type Description
Boolean Entity exists

getAbsolutePath ()String inherited

Returns the absolute path
Since:
  • 3.0.0
Returns:
Type Description
String Absolute path

getDirectory ()DKTools.IO.Directory

Returns the directory of the file
Returns:
Type Description
DKTools.IO.Directory Directory of the file

getDirectoryName ()String

Returns the directory name
Since:
  • 6.1.0
Returns:
Type Description
String Directory name

getExtension ()String inherited overrides

Returns the extension of the file
Version:
  • 10.0.8
Since:
  • 3.0.0
Returns:
Type Description
String Extension of the file

getFullName ()String inherited

Returns the full name
Version:
  • 10.0.8
Since:
  • 3.0.0
Returns:
Type Description
String Full name

getFullPath ()String inherited

Returns the full path
Version:
  • 3.0.0
Returns:
Type Description
String Full Path

getName ()String inherited

Returns the name of the entity without an extension
Since:
  • 3.0.0
Returns:
Type Description
String Name of the entity without an extension

getPath ()String inherited

Returns the path
Since:
  • 3.0.0
Returns:
Type Description
String Path

getStats (object)Object inherited

Returns a stats Returns an object with 2 properties: status - Result of an operation data - Loaded data if the status is not equal to DKTools.IO.OK then data will be null Possible statuses: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_CALLBACK_IS_NOT_AVAILABLE DKTools.IO.ERROR_OPTIONS_ARE_NOT_AVAILABLE
Name Type Description
object Object Options of an operation
Name Type Description
sync Boolean optional Use synchronous version of stat
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
Version:
  • 8.0.0
Since:
  • 2.0.0
Returns:
Type Description
Object Loaded stats

getStatsAsync ()Promise.<Object> async inherited

Returns a stats Asynchronous version of DKTools.IO.Entity.prototype.getStats Promise resolves an object with 2 properties: status - Result of an operation data - Loaded data if the status is not equal to DKTools.IO.OK then data will be null Possible statuses: DKTools.IO.OK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Version:
  • 6.1.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<Object> Loaded stats

hasExtension ()Boolean inherited

Returns true if the entity has an extension
Since:
  • 5.0.0
Returns:
Type Description
Boolean Entity has an extension

initialize (fullPath) inherited overrides

Initializes the file
Name Type Description
fullPath String Path to file
Version:
  • 6.3.0
Since:
  • 3.0.0

isAudio ()Boolean

Returns true if an extension of the file is equal to .ogg or .m4a or .rpgmvo
Version:
  • 3.0.0
Returns:
Type Description
Boolean Extension of the file is equal to .ogg or .m4a or .rpgmvo

isDirectory ()Boolean inherited

Returns true if the entity is a directory
Version:
  • 5.0.0
Since:
  • 2.0.0
Returns:
Type Description
Boolean Entity is a directory

isFile ()Boolean inherited

Returns true if the entity is a file
Version:
  • 6.2.1
Since:
  • 2.0.0
Returns:
Type Description
Boolean Entity is a file

isImage ()Boolean

Returns true if an extension of the file is equal to .png, .rpgmvp or .webp
Version:
  • 6.2.1
Returns:
Type Description
Boolean Extension of the file is equal to .png, .rpgmvp or .webp

isJson ()Boolean

Returns true if an extension of the file is equal to .json
Returns:
Type Description
Boolean Extension of the file is equal to .json

isSave ()Boolean

Returns true if an extension of the file is equal to .rpgsave
Returns:
Type Description
Boolean Extension of the file is equal to .rpgsave

isTexture ()Boolean

Returns true if an extension of the file is equal to .dds
Since:
  • 10.0.1
Returns:
Type Description
Boolean Extension of the file is equal to .dds

isTxt ()Boolean

Returns true if an extension of the file is equal to .txt
Since:
  • 5.0.0
Returns:
Type Description
Boolean Extension of the file is equal to .txt

isVideo ()Boolean

Returns true if an extension of the file is equal to .mp4 or .webm
Returns:
Type Description
Boolean Extension of the file is equal to .mp4 or .webm

load (object)Object

Loads and returns a data Returns an object with 3 properties: status - Result of an operation data - Loaded data error - Error of an operation if the status is not equal to DKTools.IO.OK then data will be null Possible statuses: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_CALLBACK_IS_NOT_AVAILABLE DKTools.IO.ERROR_OPTIONS_ARE_NOT_AVAILABLE DKTools.IO.ERROR_DECOMPRESSING_DATA DKTools.IO.ERROR_PARSING_DATA
Name Type Description
object Object Options of an operation
Name Type Description
sync Boolean optional Use synchronous version of FileSystem.readFile
options String | Object optional Options for FileSystem.readFile or FileSystem.readFileSync
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
decompress Boolean optional Use LZString.decompressFromBase64 for a data
parse Boolean | Object optional Use JSON.parse for a data
mimeType String optional Mime type (only for XMLHttpRequest)
options.encoding String optional Encoding
options.flag String optional File system flag
parse.reviver function optional A function that transforms the results
Version:
  • 8.3.0
Returns:
Type Description
Object Loaded data
Examples
const file = new DKTools.IO.File('data/System.json');
const result = file.load({ sync: true, parse: true });

if (result.status === DKTools.IO.OK) {
    console.log(result.data); // data loaded synchronously
}
const file = new DKTools.IO.File('data/System.json');

file.load({
     sync: false,
     parse: true,
     onSuccess: function(result, file) {
         if (result.status === DKTools.IO.OK) {
             console.log(result.data); // data loaded asynchronously
         }
     }
});

loadAsync (object)Promise.<{status: Number, data: (*|null)}> async

Loads and returns a data Asynchronous version of DKTools.IO.File.prototype.load Promise resolves a loaded data or null Promise resolves an object with 3 properties: status - Result of an operation data - Loaded data error - Error of an operation if the status is not equal to DKTools.IO.OK then data will be null Possible statuses: DKTools.IO.OK DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_DECOMPRESSING_DATA DKTools.IO.ERROR_PARSING_DATA
Name Type Default Description
object Object {} optional Options of an operation
Name Type Description
options String | Object optional Options for FileSystem.readFile
decompress Boolean optional Use LZString.decompressFromBase64 for a data
parse Boolean | Object optional Use JSON.parse for a data
options.encoding String optional Encoding
options.flag String optional File system flag
parse.reviver function optional A function that transforms the results
Version:
  • 7.0.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<{status: Number, data: (*|null)}> Loaded data

loadAudio ()WebAudio | null

Loads an audio file and returns a WebAudio
Version:
  • 10.0.4
Since:
  • 3.0.0
Returns:
Type Description
WebAudio | null Audio file or null

loadAudioAsync ()Promise.<(WebAudio|null)> async

Loads an audio file and returns a WebAudio Asynchronous version of DKTools.IO.File.prototype.loadAudio Promise resolves a loaded audio file or null
Since:
  • 5.0.0
Returns:
Type Description
Promise.<(WebAudio|null)> Loaded audio file or null

loadBitmap (object, hue, smooth)Bitmap | null

Loads and returns a bitmap
Name Type Description
object function | Object Function of processing after loading a bitmap or object with parameters
hue Number optional Hue of bitmap (if object is String)
smooth Boolean optional Smooth of bitmap (if object is String)
object.listener function optional Function of processing after loading a bitmap
object.hue Number optional Hue of bitmap
object.smooth Boolean optional Smooth of bitmap
Version:
  • 8.2.0
Since:
  • 3.0.0
Returns:
Type Description
Bitmap | null Loaded bitmap or null
Example
const file = new DKTools.IO.File('img/system/Window.png');
const bitmap = file.loadBitmap();

loadBitmapAsync (object, hue, smooth)Promise.<Bitmap> async

Loads and returns a bitmap Asynchronous version of DKTools.IO.File.prototype.loadBitmap Promise resolves a loaded bitmap or null
Name Type Description
object function | Object Function of processing after loading a bitmap or object with parameters
hue Number optional Hue of bitmap (if object is String)
smooth Boolean optional Smooth of bitmap (if object is String)
object.listener function optional Function of processing after loading a bitmap
object.hue Number optional Hue of bitmap
object.smooth Boolean optional Smooth of bitmap
Version:
  • 5.0.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<Bitmap> Loaded bitmap or null
Example
const file = new DKTools.IO.File('img/system/Window.png');
const bitmap = await file.loadBitmapAsync();

loadJson (object)Object

Loads the JSON data Returns an object with 3 properties: status - Result of an operation data - Loaded data error - Error of an operation if the status is not equal to DKTools.IO.OK then data will be null Possible statuses: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_CALLBACK_IS_NOT_AVAILABLE DKTools.IO.ERROR_OPTIONS_ARE_NOT_AVAILABLE DKTools.IO.ERROR_DECOMPRESSING_DATA DKTools.IO.ERROR_PARSING_DATA
Name Type Description
object Object Options of an operation
Name Type Description
sync Boolean optional Use synchronous version of FileSystem.readFile
options String | Object optional Options for FileSystem.readFile or FileSystem.readFileSync
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
decompress Boolean optional Use LZString.decompressFromBase64 for a data
parse Boolean | Object optional Use JSON.parse for a data
options.encoding String optional Encoding
options.flag String optional File system flag
parse.reviver function optional A function that transforms the results
Version:
  • 7.0.0
Since:
  • 3.0.0
Returns:
Type Description
Object Loaded data
Examples
const file = new DKTools.IO.File('data/System.json');
const result = file.loadJson({ sync: true });

if (result.status === DKTools.IO.OK) {
    console.log(result.data); // data loaded synchronously
}
const file = new DKTools.IO.File('data/System.json');

file.loadJson({
     sync: false,
     onSuccess: function(result, file) {
         if (result.status === DKTools.IO.OK) {
             console.log(result.data); // data loaded asynchronously
         }
     }
});

loadJsonAsync (object)Promise.<{status: Number, data: (*|null), error: (Error|undefined)}> async

Loads the JSON data Asynchronous version of DKTools.IO.File.prototype.loadJson Promise resolves an object with 3 properties: status - Result of an operation data - Loaded data error - Error of an operation if the status is not equal to DKTools.IO.OK then data will be null Possible statuses: DKTools.IO.OK DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_DECOMPRESSING_DATA DKTools.IO.ERROR_PARSING_DATA
Name Type Default Description
object Object {} optional Options of an operation
Name Type Description
options String | Object optional Options for FileSystem.readFile or FileSystem.readFileSync
decompress Boolean optional Use LZString.decompressFromBase64 for a data
parse Boolean | Object optional Use JSON.parse for a data
options.encoding String optional Encoding
options.flag String optional File system flag
parse.reviver function optional A function that transforms the results
Version:
  • 7.0.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<{status: Number, data: (*|null), error: (Error|undefined)}> Loaded data
Example
const file = new DKTools.IO.File('data/System.json');
const data = await file.loadJsonAsync();

remove (object)Number

Removes the file Returns a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Name Type Default Description
object Object {} optional Options of an operation
Name Type Description
sync Boolean optional Use synchronous version of FileSystem.unlink
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
Version:
  • 7.0.0
Returns:
Type Description
Number Code of the result of an operation

removeAsync ()Promise.<Number> async

Removes the file Asynchronous version of DKTools.IO.File.prototype.remove Promise resolves a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Version:
  • 7.0.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<Number> Code of the result of an operation

rename (newName, object)Number inherited

Renames the entity (file or directory) Returns a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_OVERWRITING_IS_NOT_AVAILABLE
Name Type Default Description
newName String New name of entity (file or directory) without the extension
object Object {} optional Options of an operation
Name Type Description
sync Boolean optional Use synchronous version of rename
overwrite Boolean optional Overwrite existing entity
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
Version:
  • 7.0.0
Returns:
Type Description
Number Code of the result of an operation

renameAsync (newName, object)Promise.<Number> async inherited

Renames the entity (file or directory) Asynchronous version of DKTools.IO.Entity.prototype.rename Promise resolves a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST DKTools.IO.ERROR_OVERWRITING_IS_NOT_AVAILABLE
Name Type Default Description
newName String New name of entity without the extension
object Object {} optional Options of an operation
Name Type Description
overwrite Boolean optional Overwrite existing entity
Version:
  • 7.0.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<Number> Code of the result of an operation

reserveBitmap (object, hue, smooth, reservationId)Bitmap | null

Loads, reserves and returns a bitmap
Name Type Description
object function | Object Function of processing after loading a bitmap or object with parameters
hue Number optional Hue of bitmap (if object is String)
smooth Boolean optional Smooth of bitmap (if object is String)
reservationId Number optional Reservation ID (if object is String)
object.listener function optional Function of processing after loading a bitmap
object.hue Number optional Hue of bitmap
object.smooth Boolean optional Smooth of bitmap
object.reservationId Number optional Reservation ID
Since:
  • 3.0.0
Returns:
Type Description
Bitmap | null Loaded bitmap or null

reserveBitmapAsync (object, hue, smooth, reservationId)Promise.<(Bitmap|null)> async

Loads, reserves and returns a bitmap Asynchronous version of DKTools.IO.File.prototype.reserveBitmap Promise resolves a loaded bitmap or null
Name Type Description
object function | Object Function of processing after loading a bitmap or object with parameters
hue Number optional Hue of bitmap (if object is String)
smooth Boolean optional Smooth of bitmap (if object is String)
reservationId Number optional Reservation ID (if object is String)
object.listener function optional Function of processing after loading a bitmap
object.hue Number optional Hue of bitmap
object.smooth Boolean optional Smooth of bitmap
object.reservationId Number optional Reservation ID
Since:
  • 4.0.0
Returns:
Type Description
Promise.<(Bitmap|null)> Loaded bitmap or null

save (data, object)Number

Saves the data Returns a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Name Type Default Description
data * Data to save
object Object {} optional Options of an operation
Name Type Description
stringify Boolean | Object optional Use JSON.stringify for the data
compress Boolean optional Use LZString.compressToBase64 for the data
createDirectory Boolean | Object optional Create a directory for the file
sync Boolean optional Use synchronous version of FileSystem.writeFile
options String | Object optional Options for FileSystem.writeFile or FileSystem.writeFileSync
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
stringify.replacer function | Array optional A function that transforms the results
stringify.space Number | String optional Insert white space into the output JSON string for readability purposes
createDirectory.options Object optional Options for FileSystem.mkdir or FileSystem.mkdirSync
Name Type Description
recursive Boolean optional Parent folders should be created
mode Number | String optional Directory permission
Version:
  • 7.0.0
Returns:
Type Description
Number Code of the result of an operation
Examples
const file = new DKTools.IO.File('test.txt');
const status = file.save('Hello world!', { sync: true });

if (status === DKTools.IO.OK) {
    console.log('saved!'); // data saved synchronously
}
const file = new DKTools.IO.File('test.txt');

file.save('Hello world!', {
     sync: false,
     onSuccess: function(status, file) {
         if (status === DKTools.IO.OK) {
             console.log('saved!'); // data saved asynchronously
         }
     }
});

saveAsync (data, object)Promise.<Number> async

Saves the data Asynchronous version of DKTools.IO.File.prototype.save Promise resolves a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Name Type Default Description
data * Data to save
object Object {} optional Options of an operation
Name Type Description
stringify Boolean optional Use JSON.stringify for the data
compress Boolean optional Use LZString.compressToBase64 for the data
createDirectory Boolean optional Create a directory for the file
options String | Object optional Options for FileSystem.writeFile
stringify.replacer function | Array optional A function that transforms the results
stringify.space Number | String optional Insert white space into the output JSON string for readability purposes
Version:
  • 6.1.0
Since:
  • 4.0.0
Returns:
Type Description
Promise.<Number> Code of the result of an operation

saveJson (data, object)Number

Saves the JSON data Returns a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.EXPECT_CALLBACK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Name Type Default Description
data Array | Object Data to save
object Object {} optional Options of an operation
Name Type Description
stringify Object optional Use JSON.stringify for the data
compress Boolean optional Use LZString.compressToBase64 for the data
createDirectory Boolean optional Create a directory for the file
sync Boolean optional Use synchronous version of FileSystem.writeFile
options String | Object optional Options for FileSystem.writeFile
onSuccess function optional Callback function upon completion of an operation (only for object.sync == false)
onError function optional Callback function upon completion of an operation with error (only for object.sync == false)
stringify.replacer function | Array optional A function that transforms the results
stringify.space Number | String optional Insert white space into the output JSON string for readability purposes
Since:
  • 6.1.0
Returns:
Type Description
Number Code of the result of an operation
Examples
const data = { message: 'Hello world' };
const file = new DKTools.IO.File('test.json');
const status = file.saveJson(data, { sync: true });

if (status === DKTools.IO.OK) {
    console.log('saved!'); // data saved synchronously
}
const data = { message: 'Hello world' };
const file = new DKTools.IO.File('test.json');

file.saveJson(data, {
     sync: false,
     onSuccess: function(status, file) {
         if (status === DKTools.IO.OK) {
             console.log('saved!'); // data saved asynchronously
         }
     }
});

saveJsonAsync (data, object)Promise.<Number> async

Saves the JSON data Asynchronous version of DKTools.IO.File.prototype.saveJson Promise resolves a code of the result of an operation Possible results: DKTools.IO.OK DKTools.IO.ERROR_NOT_LOCAL_MODE DKTools.IO.ERROR_PATH_DOES_NOT_EXIST
Name Type Default Description
data Array | Object Data to save
object Object {} optional Options of an operation
Name Type Description
stringify Object optional Use JSON.stringify for the data
compress Boolean optional Use LZString.compressToBase64 for the data
createDirectory Boolean optional Create a directory for the file
options String | Object optional Options for FileSystem.writeFile
stringify.replacer function | Array optional A function that transforms the results
stringify.space Number | String optional Insert white space into the output JSON string for readability purposes
Since:
  • 6.1.0
Returns:
Type Description
Promise.<Number> Code of the result of an operation