Package de.elo.ix.jscript
Class DiscFile
java.lang.Object
de.elo.ix.jscript.FileBase
de.elo.ix.jscript.DiscFile
This class provides convenient methods to read and write from files.
Write Text File
The following lines show how to write a text file. The characters are encode in the UTF-16LE format. The appropriate Byte Order Mark (BOM) is inserted at the beginning of the file.
var textFile = new Packages.de.elo.ix.jscript.DiscFile("c:\\mytext.txt", "UTF-16LE");
textFile.writeAllText(content);
Read Text File
The following lines show how to read a text file. If the file starts with a Byte Order Mark, the characters are read with the related encoding.
var textFile = new Packages.de.elo.ix.jscript.DiscFile("c:\\mytext.txt");
var content = textFile.readAllText();
Move or rename a File
The function renameTo can be used to rename or move a file.
var file = new Packages.de.elo.ix.jscript.DiscFile("c:\\mytext.txt");
file.renameTo("c:\\temp\\mytext1.txt");
Delete a File
The function remove deletes a file.
var file = new Packages.de.elo.ix.jscript.DiscFile("c:\\mytext.txt");
if (file.exists()) {
if (!file.remove()) {
throw "Cannot remove file=" + file);
}
}
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class de.elo.ix.jscript.FileBase
copyAllBytes, copyAllText, getCharset, isUseBom, readAllBytes, readAllText, setCharset, setUseBom, writeAllBytes, writeAllText
-
Constructor Details
-
DiscFile
Constructor This object uses the default character set when writing a text file. The Byte Order Mark is dectected, if a text file is to be read.- Parameters:
fileName
- File name- See Also:
-
DiscFile
public DiscFile()Constructor. This constructor prepares a temporary file that is created in the write functions. It uses the default character set when writing a text file. -
DiscFile
Constructor. This object uses the giben character set when writing a text file. The Byte Order Mark is dectected, if a text file is to be read.- Parameters:
fileName
- File namecharset
- Optional. Default character set to be used in read and write functions.- See Also:
-
-
Method Details
-
exists
public boolean exists()Returns true, if the file exists.- Returns:
- true, if the file exists.
-
remove
public boolean remove()DeletesfileName
. The function returns true, if the file or directory has been deleted. If the file or directory cannot be deleted, the function returns false and does not throw an exception.- Returns:
- true, if the file has been deleted.
-
renameTo
Renames or moves a file. The function returns true, if the file or directory has been renamed. If the file or directory cannot be deleted, the function returns false or throws an exception. The directory hierarchy of the destination file is created if it does not exist.- Parameters:
newFileName
- Destination file.- Returns:
- true, if the file has been renamed or moved.
-
toString
Returns the file name. -
setName
Set the file name.- Parameters:
fileName
- File name
-
getName
Get the file name.- Returns:
- File name
-