- Start Katalon Studio
- Click File > New > Keyword
- Browse for the target Package or create a new one for example, tools
- Input a new “Class Name” for example, WriteFile
- Click OK
- Input the following code into the method (the @Keyword annotation is not required)
//Use "CTRL+SHIFT+O" to add imports
package tools
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class WriteFile {
private String path;
private boolean append_to_file = false;
//Writeover file
public WriteFile (String file_path) {
path = file_path; }
//Appends to file
public WriteFile (String file_path, boolean append_value){
path = file_path;
append_to_file = append_value;
}
public void WriteToFile (String textline) throws IOException {
FileWriter write = new FileWriter(path, append_to_file);
PrintWriter print_line = new PrintWriter(write);
print_line.printf("%s" + "%n", textline);
print_line.close(); }
}
- To append to the file, add the following to the body of the test case
//Use "CTRL+SHIFT+O" to add imports
import tools.WriteFile as WriteFile
String file_name = "C:\\Folder_Name\\OutPut.txt"
WriteFile data = new WriteFile(file_name, true)
data.WriteToFile("This-is-a-line-of-text")
println("One record written to file...")
- To overwrite the file, add the following to the body of the test case
//Use "CTRL+SHIFT+O" to add imports
import tools.WriteFile as WriteFile
String file_name = "C:\\Folder_Name\\OutPut.txt"
WriteFile data = new WriteFile(file_name)
data.WriteToFile("This-is-a-line-of-text")
println("One record written to file...")
The full Tutorial can be found here: java for complete beginners - writing to text files