How would I read from a text file


#1

My setup:
Windows 7, 64 bit
Katalon Studio, Version: 6.1.5, Build: 3

When i run the following I get an error as listed below:

import com.sun.jna.platform.FileUtils

File file = new File("C:\\Users\\devers\\Downloads\\TestFiles\\dave.txt")
String text = FileUtils.readFileToString(file)
println text

Error:

06-11-2019 10:55:47 AM Test Cases/0 HOW_TO_CASES/27 How-to read from file
Elapsed time: 0.233s
Test Cases/0 HOW_TO_CASES/27 How-to read from file FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.sun.jna.platform.FileUtils.readFileToString() is applicable for argument types: (java.io.File) values: [C:\Users\devers\Downloads\TestFiles\dave.txt]
	at 27 How-to read from file.run(27 How-to read from file:4)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1560275745973.run(TempTestCase1560275745973.groovy:21)

#2

Try…

File file = new File("C:\\Users\\devers\\Downloads\\TestFiles\\dave.txt")
println file.getText("UTF-8")

This uses the native java.io.File


#3

my advice:
kindly ugrdade to an up to date microsoft OS or migrate to *nix


#4

hi,

this is one way to read .txt file

TestCase
import com.kms.katalon.core.util.KeywordUtil

String path = System.getProperty("user.dir")+"\\demoData.txt";

List l = CustomKeywords.'readFile.read.readFileInList'(path)

String s = l.get(1);
KeywordUtil log = new KeywordUtil()
log.logInfo("DEBUG value is "+s)

  Iterator<String> itr = l.iterator();
  while (itr.hasNext()){
	System.out.println(itr.next());
  }

Keyword
import java.nio.charset.StandardCharsets;
import java.nio.file.*;

public class read {

	@Keyword
	public List<String> readFileInList(String fileName) {
		List<String> lines = Collections.emptyList();
		try {
			lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return lines;
	}
}

#5

Thanks guys!

I used Russ’ suggestion which works perfectly.

File file = new File("C:\\Users\\devers\\Downloads\\TestFiles\\dave.txt")
String FileContents = file.getText("UTF-8")
println FileContents

#6

Come on Ibus, please play nice.

Win7 is a supported OS by both Microsoft and Katalon right now. Kalaton may even support it beyond its MS support life.

I use Win7 and will continue to do so until I’m literally forced, kicking and screaming, to use Win10+.


#7

I do play nice.
I know how addictive is w7 but unfortunately M$ has different opinions. you will see … this year may be the last for his life.
i did use it too, for long time, since the early begining.
lucky me, at home i migrated everything i own to linux and at work i have w10 provided by the company


#8

Currently, end of life is Jan 2020.


#9

april 2020 if i remember it right. so, it’ less than a year … you still have time to find alternate options
ok, is not this year but still less than one year