public static class SAFSPlus.Files
extends java.lang.Object
If you meet some errors when calling these API, please try to run
SAFSPlus.DriverCommand.Expressions(boolean)
to turn off the expression as
Misc.Expressions(false);
and then call the string method
Files.xxx();
Constructor and Description |
---|
Files() |
Modifier and Type | Method and Description |
---|---|
static boolean |
CloseFile(java.lang.String fileNo)
Close an opened file.
|
static boolean |
CopyFile(java.lang.String source,
java.lang.String dest)
Copy the content of source file to target file.
|
static boolean |
CopyMatchingFiles(java.lang.String fromDirectory,
java.lang.String toDirectory,
java.lang.String pattern,
FileUtilities.PatternFilterMode mode)
Copy multiple files/sub-directories, based on matching the provided pattern, from one directory to another.
|
static boolean |
CreateDirectory(java.lang.String directory)
Create a new directory.
|
static boolean |
CreateFile(java.lang.String file,
FileUtilities.Mode mode,
FileUtilities.Access access,
java.lang.String fileNoVar,
java.lang.String... optionals)
Create and open a new file with the filename, mode and access provided.
Note: After calling this method, DO NOT forget to Close: Files.CloseFile(fileNo); Note: For parameter Mode and Access, only a few combinations are permitted, as following: FileUtilities.Access.W , FileUtilities.Mode.OUTPUT
FileUtilities.Access.W , FileUtilities.Mode.APPEND
FileUtilities.Access.R , FileUtilities.Mode.INPUT |
static boolean |
DeleteDirectory(java.lang.String directory)
Delete the directory itself, ONLY EMPTY directory can be deleted.
|
static boolean |
DeleteDirectoryContents(java.lang.String directoryName)
Delete recursively the contents (files and sub-directories), the directory itself is kept.
|
static boolean |
DeleteDirectoryContents(java.lang.String directory,
boolean delectDirectory)
Delete recursively the contents (files and sub-directories) of a provided directory.
|
static boolean |
DeleteFile(java.lang.String file,
boolean verifyExistence)
Delete file.
|
(package private) static boolean |
FilterImage(java.lang.String source,
java.lang.String dest,
FileUtilities.ImageFilterMode mode,
java.lang.String filter)
Filter out specific parts of an image and save to an image file.
|
static boolean |
FilterImage(java.lang.String source,
java.lang.String dest,
java.util.List<ImageUtils.SubArea> subareas)
Filter out specific parts of an image by coordinates and save to an image file.
The filtered area will be covered by black color. |
static boolean |
FilterImage(java.lang.String source,
java.lang.String dest,
java.lang.String subareas)
Filter out specific parts of an image by coordinates and save to an image file.
The filtered area will be covered by black color. |
(package private) static boolean |
FilterTextFile(java.lang.String file,
FileUtilities.PatternFilterMode mode,
java.lang.String pattern,
java.lang.String replace,
boolean caseSensitive,
java.lang.String... optionls)
Filter a text file based on the given parameters.
|
static boolean |
FilterTextFile(java.lang.String file,
java.lang.String regexPattern,
java.lang.String replace,
boolean caseSensitive,
java.lang.String... optionals)
Filter a text file based on the given parameters.
|
static boolean |
FilterTextFile(java.lang.String file,
java.lang.String regexPattern,
java.lang.String replace,
java.lang.String... optionals)
Filter a text file based on the given parameters.
|
static boolean |
GetFileDateTime(java.lang.String name,
java.lang.String resultVariable)
Get the file's LastModified date time and save it to a variable, the time is in 12 AM PM format.
|
static boolean |
GetFileDateTime(java.lang.String file,
java.lang.String resultVariable,
boolean isMilitaryFormat,
FileUtilities.DateType dateType)
Get the file date time and save it to a variable.
Note: For Operating System other than Windows, only "lastModified" time is supported. |
static boolean |
GetFileProtections(java.lang.String file,
java.lang.String resultVariable)
Save the file attributes for the file name to the variable provided.
|
static boolean |
GetFiles(java.lang.String directory,
java.lang.String outputFile)
Search the directory for normal files and write the found filenames into an output file.
It is equivalent to call #GetFiles(String, String, FileAttribute.instance()) |
static boolean |
GetFiles(java.lang.String directory,
java.lang.String outputFile,
FileUtilities.FileAttribute attribute)
Search the directory for files according to file attribute and write the found filenames into an output file.
|
static boolean |
GetFileSize(java.lang.String file,
java.lang.String resultVariable)
Determine the file size.
|
static boolean |
GetINIFileValue(java.lang.String file,
java.lang.String section,
java.lang.String item,
java.lang.String resultVariable)
Get value from INI file.
|
static boolean |
GetStringCountInFile(java.lang.String file,
java.lang.String token,
java.lang.String... optionals)
Count the number of occurrence of a token in a file and store that number in a variable.
|
static boolean |
GetSubstringsInFile(java.lang.String file,
java.lang.String regexStart,
java.lang.String regexStop,
java.lang.String resultVarRoot)
Extract dynamic substrings from a file using regular expressions.
|
static boolean |
GetTextFromImage(java.lang.String imageFileName,
java.lang.String resultVar,
java.lang.String... optionals)
Incorporate OCR technology to detect the text in an image file and save the text to a variable.
|
static boolean |
IfExistDir(java.lang.String directory,
java.lang.String... fileCommandAndParams)
If the specified directory exists, then execute the following driver command.
|
static boolean |
IfExistFile(java.lang.String file,
java.lang.String... fileCommandAndParams)
If the specified file exists, then execute the following driver command.
|
static boolean |
IsEndOfFile(java.lang.String fileNo,
java.lang.String resultVar)
Test if the opened file is at the end and store the result to a variable.
|
static boolean |
OpenFile(java.lang.String file,
FileUtilities.Mode mode,
FileUtilities.Access access,
java.lang.String fileNoVar,
java.lang.String... fileNo)
Open an file with the filename, mode and access provided.
Note: After calling this method, DO NOT forget to Close: Files.CloseFile(fileNo); Note: For parameter Mode and Access, only a few combinations are permitted, as following: FileUtilities.Access.W , FileUtilities.Mode.OUTPUT
FileUtilities.Access.W , FileUtilities.Mode.APPEND
FileUtilities.Access.R , FileUtilities.Mode.INPUT |
static boolean |
OpenUTF8File(java.lang.String file,
FileUtilities.Mode mode,
FileUtilities.Access access,
java.lang.String fileNoVar,
java.lang.String... optionals)
Open an UTF-8 file with the filename, mode and access provided.
Note: After calling this method, DO NOT forget to Close: Files.CloseFile(fileNo); Note: For parameter Mode and Access, only a few combinations are permitted, as following: FileUtilities.Access.W , FileUtilities.Mode.OUTPUT
FileUtilities.Access.W , FileUtilities.Mode.APPEND
FileUtilities.Access.R , FileUtilities.Mode.INPUT |
static boolean |
PrintToFile(java.lang.String fileNo,
java.lang.String content)
Write a string directly to a opened file defined by file number.
It is equivalent to call #PrintToFile(String, String, Placement.NEWLINE) |
static boolean |
PrintToFile(java.lang.String fileNo,
java.lang.String content,
FileUtilities.Placement placement)
Write a string to a opened file defined by file number.
|
static boolean |
ReadFileChars(java.lang.String fileNo,
int charsToRead,
java.lang.String resultVar)
Read the number of characters from the file defined by file number and assign them to a variable.
|
static boolean |
ReadFileLine(java.lang.String fileNo,
java.lang.String resultVar)
Read a line from the file defined by file number and assign it to a variable.
|
static boolean |
RenameFile(java.lang.String oldName,
java.lang.String newName,
boolean verifyExistence)
Rename the file from the old file name to the new filename.
|
static boolean |
SaveTextFromImage(java.lang.String imageFileName,
java.lang.String resultFile,
java.lang.String... optionals)
Incorporate OCR technology to detect the text in an image file and save the text to a text file.
|
static boolean |
SetFileProtections(java.lang.String file,
FileUtilities.FileAttribute attribute)
Change the file attribute to the value of the new file protection provided.
|
static boolean |
WriteFileChars(java.lang.String fileNo,
int charsToWrite,
java.lang.String content)
Write the specified number of characters to a file already opened for writing.
|
public static boolean CloseFile(java.lang.String fileNo)
fileNo
- String, the file number of an opened file.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
if(Files.CloseFile(fileNo))
System.out.println("file '"+fileNo+"' has been closed.");
public static boolean CopyFile(java.lang.String source, java.lang.String dest)
source
- String, the name of source file.dest
- String, the name of destination file.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory";
String file = directory+File.separator+"new test file3.txt";
String dest = directory+File.separator+"copy of file3.txt";
Files.CopyFile(file, dest);
public static boolean CopyMatchingFiles(java.lang.String fromDirectory, java.lang.String toDirectory, java.lang.String pattern, FileUtilities.PatternFilterMode mode)
fromDirectory
- String, the name of directory from where to find matching files.toDirectory
- String, the name of directory to where to copy the matching files.pattern
- String, the pattern used to find matching files.mode
- PatternFilterMode, the mode of the searching pattern ("regex", "wildcast").SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory";
String toDirectory = "C:\\Users\\Lei Wang\\TestDriverCommand\\Copy Directory";
Files.CopyMatchingFiles(directory, toDirectory, "[a-z ]*f.*", PatternFilterMode.REGEXP);
Files.CopyMatchingFiles(directory, toDirectory, "UTF*.*", PatternFilterMode.WILDCARD);
public static boolean CreateDirectory(java.lang.String directory)
directory
- String, the name of directory to create.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory";
if(Files.CreateDirectory(directory))
System.out.println(directory+" is created.");
public static boolean CreateFile(java.lang.String file, FileUtilities.Mode mode, FileUtilities.Access access, java.lang.String fileNoVar, java.lang.String... optionals)
FileUtilities.Access.W
, FileUtilities.Mode.OUTPUT
FileUtilities.Access.W
, FileUtilities.Mode.APPEND
FileUtilities.Access.R
, FileUtilities.Mode.INPUT
file
- String, the name of file to create.mode
- Mode, the mode (input, output, append) used to create fileaccess
- Access, the access (write, read) used to create filefileNoVar
- String, the variable where stored the file number of created file.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory";
String file = directory+File.separator+"new test file3.txt";
if(Files.CreateFile(file, Mode.OUTPUT, Access.W, result)){
System.out.println(file+" is created and opened with file number '"+GetVariableValue(result)+"'");
Files.CloseFile(GetVariableValue(result));
}
public static boolean DeleteDirectory(java.lang.String directory)
directory
- String, the name of directory to delete.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\Empty Folder";
if(Files.CreateDirectory(directory)){
if(Files.DeleteDirectory(directory)){
System.out.println("directory '"+directory+"' has been deleted.");
}
}
public static boolean DeleteDirectoryContents(java.lang.String directoryName)
directory
- String, the name of directory to delete.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\To Delete Folder";
if(Files.CreateDirectory(directory)){
String newFile = directory+File.separator+"\\test.file";
Files.CreateFile(newFile, Mode.OUTPUT, Access.W, result);
Files.CloseFile(GetVariableValue(result));
if(Files.DeleteDirectory(directory)){
System.err.println("directory '"+directory+"' is not empty, but it is deleted! NOT possible.");
}else{
System.out.println("directory '"+directory+"' is not empty, can not be deleted. OK.");
}
if(Files.DeleteDirectoryContents(directory)){
System.out.println("directory '"+directory+"' its content has been delete.");
}
if(Files.DeleteDirectory(directory)){//directory is empty, can be deleted.
System.out.println("directory '"+directory+"' is deleted.");
}
}
public static boolean DeleteDirectoryContents(java.lang.String directory, boolean delectDirectory)
directory
- String, the name of directory to delete.delectDirectory
- boolean, true delete also the directory itself; otherwise the directory is kept.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\To Delete Folder";
if(Files.CreateDirectory(directory)){
String newFile = directory+File.separator+"\\test.file";
Files.CreateFile(newFile, Mode.OUTPUT, Access.W, result);
Files.CloseFile(GetVariableValue(result));
if(Files.DeleteDirectoryContents(directory, true)){
System.out.println("directory '"+directory+"' has been completely delete.");
}
}
public static boolean DeleteFile(java.lang.String file, boolean verifyExistence)
file
- String, the name of file to delete.verifyExistence
- boolean, true verify the existence of the file before deleting.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
if(Files.DeleteFile(file, true))
System.out.println("file '"+file+"' has been deleted.");
public static boolean FilterImage(java.lang.String source, java.lang.String dest, java.util.List<ImageUtils.SubArea> subareas)
source
- String, the name of source image file.dest
- String, the name of the destination image file.subareas
- ListSAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String image = "C:\\Users\\Lei Wang\\TestDriverCommand\\keyword.png";
String subImage = "C:\\Users\\Lei Wang\\TestDriverCommand\\subimage.png";
List<SubArea> subareas = new ArrayList<SubArea>();
subareas.add(new SubArea(0,0,"20%","30%"));
subareas.add(new SubArea("50%", "50%", "60%", "70%"));
subareas.add(new SubArea("80%", "80%", "90%", "90%"));
Files.FilterImage(image, subImage, subareas);//filter 3 areas
public static boolean FilterImage(java.lang.String source, java.lang.String dest, java.lang.String subareas)
source
- String, the name of source image file.dest
- String, the name of the destination image file.subareas
- String, the subareas used to filter image, SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String image = "C:\\Users\\Lei Wang\\TestDriverCommand\\keyword.png";
String subImage = "C:\\Users\\Lei Wang\\TestDriverCommand\\subimage.png";
String coords = "0,0,70%,50%";//DO NOT put any space between coordinate
Files.FilterImage(image, subImage, coords);//filter 1 area
subImage = "C:\\Users\\Lei Wang\\TestDriverCommand\\subimage2.png";
coords = "0,0,70%,50% 80%;80%;100%;100%";
Files.FilterImage(image, subImage, coords);//filter 2 areas
static boolean FilterImage(java.lang.String source, java.lang.String dest, FileUtilities.ImageFilterMode mode, java.lang.String filter)
source
- String, the name of source image file.dest
- String, the name of the destination image file.mode
- ImageFilterMode, the mode of filter. For example, if it is FileUtilities.ImageFilterMode.COORD
, the filter means the coordinates.filter
- String, the filter used to filter image.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
public static boolean FilterTextFile(java.lang.String file, java.lang.String regexPattern, java.lang.String replace, java.lang.String... optionals)
file
- String, the name of file to filter.regexPattern
- String, the regular expression used to find matching stringreplace
- String, the token used to replace the matching stringoptionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String regexPattern = "saturday|sat|sunday|sun";
String replace = "weekend";
Files.FilterTextFile(file, regexPattern, replace);
public static boolean FilterTextFile(java.lang.String file, java.lang.String regexPattern, java.lang.String replace, boolean caseSensitive, java.lang.String... optionals)
file
- String, the name of file to filter.regexPattern
- String, the regular expression used to find matching stringreplace
- String, the token used to replace the matching stringcaseSensitive
- boolean, if matching is sensitive.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String regexPattern = "saturday|sat|sunday|sun";
String replace = "weekend";
Files.FilterTextFile(file, regexPattern, replace, false);
static boolean FilterTextFile(java.lang.String file, FileUtilities.PatternFilterMode mode, java.lang.String pattern, java.lang.String replace, boolean caseSensitive, java.lang.String... optionls)
file
- String, the name of file to filter.mode
- PatternFilterMode, the pattern mode ("regex" or "wildcast")pattern
- String, the pattern used to find matching stringreplace
- String, the token used to replace the matching stringcaseSensitive
- boolean, if matching is sensitive.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
public static boolean GetFileDateTime(java.lang.String name, java.lang.String resultVariable)
file
- String, The file to get attribute.resultVariable
- String, The variable to save file date time.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\new test file3.txt";
if(Files.GetFileDateTime(file, result))
System.out.println("file '"+file+"', LastModified time is "+GetVariableValue(result));
public static boolean GetFileDateTime(java.lang.String file, java.lang.String resultVariable, boolean isMilitaryFormat, FileUtilities.DateType dateType)
file
- String, The file to get attribute.resultVariable
- String, The variable to save file date time.isMilitaryFormat
- boolean, if true, time is in military format (24-hours), otherwise is 12-hours AM PM format.dateType
- DateType, The date type ("created", "lastModified", "lastAccessed")SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\new test file3.txt";
if(Files.GetFileDateTime(file, result, true, DateType.LASTACCESSED))
System.out.println("file '"+file+"', LastAccessed military time is "+GetVariableValue(result));
if(Files.GetFileDateTime(file, result, true, DateType.CREATED))
System.out.println("file '"+file+"', Created military time is "+GetVariableValue(result));
if(Files.GetFileDateTime(file, result, false, DateType.LASTMODIFIED))
System.out.println("file '"+file+"', LastModified time is "+GetVariableValue(result));
public static boolean GetFileProtections(java.lang.String file, java.lang.String resultVariable)
file
- String, The file to get attribute.resultVariable
- String, The variable to save file attribute.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
file = "C:\\Users\\Lei Wang\\ntuser.pol";
Files.GetFileProtections(file, result);
int attributes = Integer.parseInt(GetVariableValue(result));
FileAttribute attribute = FileAttribute.instance(attributes);
System.out.println("File Protection: "+attribute.toString());
public static boolean GetFiles(java.lang.String directory, java.lang.String outputFile, FileUtilities.FileAttribute attribute)
directory
- String, the name of directory where to search file.outputFile
- String, the name of file to store the found files name.attribute
- FileAttribute, the file attribute served as search condition.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\";
String directoryToCheck = "C:\\Users\\Lei Wang";
Files.GetFiles(directoryToCheck, directory+"normalList2.txt");
Files.GetFiles(directoryToCheck, directory+"normalList.txt", FileAttribute.instance());
Files.GetFiles(directoryToCheck, directory+"archiveList.txt", new FileAttribute(Type.ARCHIVEFILE));
Files.GetFiles(directoryToCheck, directory+"hiddenList.txt", new FileAttribute(Type.HIDDENFILE));
Files.GetFiles(directoryToCheck, directory+"archiveAndHiddenList.txt", new FileAttribute(Type.HIDDENFILE).add(Type.ARCHIVEFILE));
public static boolean GetFiles(java.lang.String directory, java.lang.String outputFile)
#GetFiles(String, String, FileAttribute.instance())
directory
- String, the name of directory where to search file.outputFile
- String, the name of file to store the found files name.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String directory = "C:\\Users\\Lei Wang\\TestDriverCommand\\";
String directoryToCheck = "C:\\Users\\Lei Wang";
Files.GetFiles(directoryToCheck, directory+"normalList2.txt");
public static boolean GetFileSize(java.lang.String file, java.lang.String resultVariable)
file
- String, the name of file to get size.resultVariable
- String, The variable to store the size value.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
file = "C:\\Users\\Lei Wang\\ntuser.pol";
if(Files.GetFileSize(file, result))
System.out.println("file '"+file+"', size is "+GetVariableValue(result));
public static boolean GetINIFileValue(java.lang.String file, java.lang.String section, java.lang.String item, java.lang.String resultVariable)
section
- String, the name of section in INI file.item
- String, the name of item in INI file.resultVariable
- String, The variable to store the item value.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "D:\\test.ini";//Suppose it contains the content as above example
String result = "result";
String section = "Section1";
String item = "Item1";
if(Files.GetINIFileValue(file, section, item, result))
System.out.println("ini file '"+file+"' '"+section+":"+item+"'="+GetVariableValue(result));//"value_A"
section = "Section2";
if(Files.GetINIFileValue(file, section, item, result))
System.out.println("ini file '"+file+"' '"+section+":"+item+"'="+GetVariableValue(result));//"value_D"
public static boolean GetStringCountInFile(java.lang.String file, java.lang.String token, java.lang.String... optionals)
file
- String, the file in which the token will be searched.token
- String, the token to searchoptionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String var = "result";
String file = "fileName.txt";
String token = "aBc";
if(Files.GetStringCountInFile(file, token, var))
//number of occurrence of "aBc" in file "fileName.txt"
System.out.println("GetStringCountInFile success: result is "+GetVariableValue(var));
if(Files.GetStringCountInFile(file, token, var, "false"))
//number of occurrence of "aBc"/"abc" in file "fileName.txt"
System.out.println("GetStringCountInFile success: result is "+GetVariableValue(var));
public static boolean GetSubstringsInFile(java.lang.String file, java.lang.String regexStart, java.lang.String regexStop, java.lang.String resultVarRoot)
file
- String, the name of file to find string.regexStart
- String, The starting regular expression. Should not be empty.regexStop
- String, The stopping regular expression. Should not be empty.resultVarRoot
- String, The variable root name to contain the found string and count.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
//List all methods name appeared in the file
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String regexStart = "\\.";
String regexStop = "\\(";
String result = "Method";
String counter = result+"Count";
if(Files.GetSubstringsInFile(file, regexStart, regexStop, result)){
int count = Integer.parseInt(GetVariableValue(counter));
for(int i=0;i<count;i++)
System.out.println(GetVariableValue(result+(i+1)));
}
public static boolean GetTextFromImage(java.lang.String imageFileName, java.lang.String resultVar, java.lang.String... optionals)
imageFileName
- String, the image file containing text to detect.resultVar
- String, the variable to store the detected text.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String image = "C:\\Users\\Lei Wang\\TestDriverCommand\\keyword.png";
if(Files.GetTextFromImage(image, result, OCREngine.OCR_T_ENGINE_KEY, Locale.ENGLISH.getLanguage(), "3.0"))
pass("image text (translated by TOCR) is '"+GetVariableValue(result)+"'");
if(Files.GetTextFromImage(image, result, OCREngine.OCR_G_ENGINE_KEY, Locale.ENGLISH.getLanguage(), "2.0"))
pass("image text (translated by GOCR) is '"+GetVariableValue(result)+"'");
public static boolean IfExistDir(java.lang.String directory, java.lang.String... fileCommandAndParams)
directory
- String, the name of directory to test the existence.fileCommandAndParams
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String toDirectory = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory";
String newdir = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\NewDirectory";
String fileDriverCommand = "CreateDirectory";
if(Files.IfExistDir(directory, fileDriverCommand, newdir))
pass("directory '"+directory+"' DOES exist, and '"+fileDriverCommand+"' has executed.");
public static boolean IfExistFile(java.lang.String file, java.lang.String... fileCommandAndParams)
file
- String, the name of file to test the existence.fileCommandAndParams
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\new test file3.txt";
String destination = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\second copy of file3.txt";
String fileDriverCommand = "CopyFile";
if(Files.IfExistFile(file, fileDriverCommand, file, destination))
pass("file '"+file+"' DOES exist and it has been copied to '"+destination+"'");
public static boolean IsEndOfFile(java.lang.String fileNo, java.lang.String resultVar)
fileNo
- String, the file number of the opened file.resultVar
- String, the variable to store the result.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\new test file3.txt";
String fileNo = null;
if(Files.OpenFile(file, Mode.INPUT, Access.R, result)){
fileNo = GetVariableValue(result);
String line = null;
while(true){
Files.IsEndOfFile(fileNo, result);
if(Boolean.parseBoolean(GetVariableValue(result))) break;
Files.ReadFileLine(fileNo, result);
line = GetVariableValue(result);//do some thing with the line.
}
if(Files.CloseFile(fileNo))
System.out.println("file '"+fileNo+"' has been closed.");
}
public static boolean OpenFile(java.lang.String file, FileUtilities.Mode mode, FileUtilities.Access access, java.lang.String fileNoVar, java.lang.String... fileNo)
FileUtilities.Access.W
, FileUtilities.Mode.OUTPUT
FileUtilities.Access.W
, FileUtilities.Mode.APPEND
FileUtilities.Access.R
, FileUtilities.Mode.INPUT
file
- String, the name of file to open.mode
- Mode, the mode (input, output, append) used to open fileaccess
- Access, the access (write, read) used to open filefileNoVar
- String, the variable where stored the file number of opened file.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\new test file3.txt";
if(Files.OpenFile(file, Mode.INPUT, Access.R, result))
System.out.println(file+" has been opened with file number '"+GetVariableValue(result)+"' for input.");
public static boolean OpenUTF8File(java.lang.String file, FileUtilities.Mode mode, FileUtilities.Access access, java.lang.String fileNoVar, java.lang.String... optionals)
FileUtilities.Access.W
, FileUtilities.Mode.OUTPUT
FileUtilities.Access.W
, FileUtilities.Mode.APPEND
FileUtilities.Access.R
, FileUtilities.Mode.INPUT
file
- String, the name of file to open.mode
- Mode, the mode (input, output, append) used to open fileaccess
- Access, the access (write, read) used to open filefileNoVar
- String, the variable where stored the file number of opened file.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\UTF8 FILE.txt";
if(Files.OpenUTF8File(file, Mode.OUTPUT, Access.W, result))
System.out.println(file+" is opened with file number '"+GetVariableValue(result)+"' for output UTF8 strings.");
public static boolean PrintToFile(java.lang.String fileNo, java.lang.String content)
#PrintToFile(String, String, Placement.NEWLINE)
fileNo
- String, The file number of the file to write.content
- Strung, The string to write.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String fileNo = null;//hold the file number for an opened file.
String content = "Print Output Placement Parameter determines where the next output to the same file should begin.";
String separator = "=================================================";
if(Files.CreateFile(file, Mode.OUTPUT, Access.W, result)){
fileNo = GetVariableValue(result);
System.out.println(file+" is created and opened with file number '"+fileNo+"' for output.");
if(Files.PrintToFile(fileNo, separator)){
System.out.println("'"+separator+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.PrintToFile(fileNo, content)){
System.out.println("'"+content+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.CloseFile(fileNo)) System.out.println("file '"+fileNo+"' has been closed.");
}
public static boolean PrintToFile(java.lang.String fileNo, java.lang.String content, FileUtilities.Placement placement)
fileNo
- String, The file number of the file to write.content
- Strung, The string to write.placement
- Placement, where to write the content. It can be one of
FileUtilities.Placement.NEWLINE
Write the string to a new line
FileUtilities.Placement.TABULATION
Write the string to the next print area
FileUtilities.Placement.IMMIDIATE
Write the string directly
SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String fileNo = null;//hold the file number for an opened file.
String content = "Print Output Placement Parameter determines where the next output to the same file should begin.";
String separator = "=================================================";
if(Files.CreateFile(file, Mode.OUTPUT, Access.W, result)){
fileNo = GetVariableValue(result);
System.out.println(file+" is created and opened with file number '"+fileNo+"' for output.");
if(Files.PrintToFile(fileNo, separator, Placement.NEWLINE)){
System.out.println("'"+separator+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.PrintToFile(fileNo, content, Placement.NEWLINE)){
System.out.println("'"+content+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.PrintToFile(fileNo, separator)){
System.out.println("'"+separator+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.PrintToFile(fileNo, content, Placement.IMMIDIATE)){
System.out.println("'"+content+"' has been written to file '"+fileNo+"' in same line immediately.");
}
if(Files.PrintToFile(fileNo, separator)){
System.out.println("'"+separator+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.PrintToFile(fileNo, content, Placement.TABULATION)){
System.out.println("'"+content+"' has been written to file '"+fileNo+"' in same line at the next print zone.");
}
if(Files.PrintToFile(fileNo, separator)){
System.out.println("'"+separator+"' has been written to file '"+fileNo+"' in a new line.");
}
if(Files.PrintToFile(fileNo, content)){
System.out.println("'"+content+"' has been written to file '"+fileNo+"' in new line.");
}
if(Files.CloseFile(fileNo)) System.out.println("file '"+fileNo+"' has been closed.");
}
public static boolean ReadFileChars(java.lang.String fileNo, int charsToRead, java.lang.String resultVar)
fileNo
- String, The file number of the file to be read from.charsToRead
- int, The number of characters to be read.resultVar
- String, the variable to store the characters read from the file.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String fileNo = null;//hold the file number for an opened file.
String content = null;
if(Files.OpenFile(file, Mode.INPUT, Access.R, result)){
fileNo = GetVariableValue(result);
System.out.println(file+" has been opened with file number '"+fileNo+"' for input.");
int charsToRead = 15;
if(Files.ReadFileChars(fileNo, charsToRead, result)){
System.out.println(charsToRead+" characters: '"+GetVariableValue(result)+"' have been read from file '"+fileNo+"'");
}
if(Files.ReadFileLine(fileNo, result)){
System.out.println(" line '"+GetVariableValue(result)+"' have been read from file '"+fileNo+"'");
}
if(Files.CloseFile(fileNo))
System.out.println("file '"+fileNo+"' has been closed.");
}
public static boolean ReadFileLine(java.lang.String fileNo, java.lang.String resultVar)
fileNo
- String, The file number of the file to be read from.resultVar
- String, the variable to store the line read from the file.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
ReadFileChars(String, int, String)
public static boolean RenameFile(java.lang.String oldName, java.lang.String newName, boolean verifyExistence)
oldName
- String, the filename to be renamed.newName
- String, the new name.verifyExistence
- boolean, true verify the existence of the file before renaming.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String newfile = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\renamed newFile.txt";
Files.RenameFile(file, newfile, true);
public static boolean SaveTextFromImage(java.lang.String imageFileName, java.lang.String resultFile, java.lang.String... optionals)
imageFileName
- String, the image file containing text to detect.resultFile
- String, the file to store the detected text.optionals
- SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String image = "C:\\Users\\Lei Wang\\TestDriverCommand\\keyword.png";
String textFile = "C:\\Users\\Lei Wang\\TestDriverCommand\\image_T.txt";
Files.SaveTextFromImage(image, textFile, OCREngine.OCR_T_ENGINE_KEY, Locale.ENGLISH.getLanguage(), "2.0");
textFile = "C:\\Users\\Lei Wang\\TestDriverCommand\\image_G.txt";
Files.SaveTextFromImage(image, textFile, OCREngine.OCR_G_ENGINE_KEY, Locale.ENGLISH.getLanguage(), "2.5");
public static boolean SetFileProtections(java.lang.String file, FileUtilities.FileAttribute attribute)
file
- String, The file to modify attribute.attribute
- FileAttribute, The attribute to set to file.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String file = "C:\\Users\\Lei Wang\\New Text Document.txt";
FileAttribute attribute = FileAttribute.instance(FileAttribute.Type.SYSTEMFILE);
attribute.add(FileAttribute.Type.READONLYFILE);
attribute.add(FileAttribute.Type.ARCHIVEFILE);
Files.SetFileProtections(file, attribute);
public static boolean WriteFileChars(java.lang.String fileNo, int charsToWrite, java.lang.String content)
fileNo
- String, The file number/identifier of the file to be written to.charsToWrite
- int, The number of characters to write to the file; A negative number means all chars.content
- String, The content to write to file.SAFSPlus.prevResults
,
TestRecordData.getStatusCode()
,
String result = "result";
String file = "C:\\Users\\Lei Wang\\TestDriverCommand\\New Directory\\newFile.txt";
String fileNo = null;//hold the file number for an opened file.
String content = "Print Output Placement Parameter determines where the next output to the same file should begin.";
int charsToWrite = 10;
if(Files.CreateFile(file, Mode.OUTPUT, Access.W, result)){
fileNo = GetVariableValue(result);
System.out.println(file+" is created and opened with file number '"+fileNo+"' for output.");
if(Files.WriteFileChars(fileNo, charsToWrite, content))
System.out.println(charsToWrite+" characters of string '"+content+"' has been written to file '"+fileNo+"'");
if(Files.CloseFile(fileNo)) System.out.println("file '"+fileNo+"' has been closed.");
}
Copyright © SAS Institute. All Rights Reserved.