Hello team, below mentioned code is working fine. After this how can I verify the data inside the downloaded file? I want to verify only first line of the excel sheet (Ex.: Column names)
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.stream.Collectors
// get the current timestamp in the format of yyyyMMdd_hhmmss
LocalDateTime now = LocalDateTime.now()
String timestamp = DateTimeFormatter.ofPattern(“yyyyMMdd_hhmmss”).format(now)
// prepare a regular expression to match the file
Pattern pattern = Pattern.compile(‘^(myFile\-)(\d{8}_\d{6})(\.txt)$’)
// find the Path of ‘Downloads’ directory of my OS user
Path userHome = Paths.get(System.getProperty(“user.home”))
Path downloads = userHome.resolve(‘Downloads’)
println(downloads.toString())
// get the set of files contained in the Downloads directory now (before insertion)
Set previousFileSet = Files.list(downloads).collect(Collectors.toSet())
// insert an anonymous file (equivalent to downloading a file from web)
Path newFile = downloads.resolve(“myFile-${timestamp}.txt”)
newFile.toFile().text = “Hello, world!”
// get the set of files contained in the Downloads directory now (after insertion)
Set currentFileSet = Files.list(downloads).collect(Collectors.toSet())
// calculate the Relative Complement of the file sets (“after” minus “before”)
Set differenceSet = new HashSet(currentFileSet)
differenceSet.removeAll(previousFileSet)
// how many files were inserted? should be 1 or more
assert differenceSet.size() >= 1
// know the file name
Path differenceFile = differenceSet.toList().get(0)
String fileName = differenceFile.getFileName().toString()
// verify the name of inserted file
Matcher matcher = pattern.matcher(fileName)
assert matcher.matches()
assert matcher.group(1) == ‘myFile-’
assert matcher.group(3) == ‘.txt’
// now we know the timestamp part of the file name inserted
println “timestamp was ${matcher.group(2)}”