Package de.elo.ix.jscript
Class DiscFile
java.lang.Object
de.elo.ix.jscript.FileBase
de.elo.ix.jscript.DiscFile
public class DiscFile extends FileBase
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
-
Method Summary
Modifier and Type Method Description boolean
exists()
Returns true, if the file exists.java.lang.String
getName()
Get the file name.boolean
remove()
DeletesfileName
.boolean
renameTo(java.lang.String newFileName)
Renames or moves a file.void
setName(java.lang.String fileName)
Set the file name.java.lang.String
toString()
Returns the file name.Methods inherited from class de.elo.ix.jscript.FileBase
copyAllBytes, copyAllText, getCharset, isUseBom, readAllBytes, readAllText, setCharset, setUseBom, writeAllBytes, writeAllText
-
Constructor Details
-
DiscFile
public DiscFile(java.lang.String fileName)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:
FileBase.setCharset(String)
,FileBase.setUseBom(boolean)
-
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
public DiscFile(java.lang.String fileName, java.lang.String charset)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:
FileBase.setCharset(String)
,FileBase.setUseBom(boolean)
-
-
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
public boolean renameTo(java.lang.String newFileName)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
public java.lang.String toString()Returns the file name.- Overrides:
toString
in classjava.lang.Object
-
setName
public void setName(java.lang.String fileName)Set the file name.- Parameters:
fileName
- File name
-
getName
public java.lang.String getName()Get the file name.- Returns:
- File name
-