我试图在Soap UI中使用以下Groovy代码行创建一个新的文本文件,但我没有看到该文件已创建
import java.io.File
def newFile = new File("C:/Users/ramus/Documents/Jars/test.txt")
Run Code Online (Sandbox Code Playgroud)
能否帮助我理解上面代码行有什么问题?
Rah*_*sai 12
初始化你的文件:
def yourFile = new File("yourFilePath.txt")
Run Code Online (Sandbox Code Playgroud)
删除已存在文件的任何记录。即使没有现有文件,这也可以工作。
yourFile.delete()
Run Code Online (Sandbox Code Playgroud)
最后,创建新文件。
yourFile.createNewFile()
Run Code Online (Sandbox Code Playgroud)
要将内容添加到文件中,您可以使用:
yourFile.text="Text content"
Run Code Online (Sandbox Code Playgroud)
在此步骤中,您的文件包含:“文本内容”
yourFile.append=" appended to my file"
Run Code Online (Sandbox Code Playgroud)
在此步骤中,您的文件包含:“附加到我的文件的文本内容”
yourFile.text="New content of your file"
Run Code Online (Sandbox Code Playgroud)
在此步骤中,您的文件包含:“文件的新内容”
小智 9
new File(path)表示创建指向具有所选路径的文件或目录的指针.使用此指针,您可以执行任何您想要创建,读取,追加等操作.如果您只想在驱动器上创建文件,您可以执行以下操作:
def newFile = new File("C:/Users/ramus/Documents/Jars/test.txt")
newFile.createNewFile()
Run Code Online (Sandbox Code Playgroud)
只是已接受答案的补充-如果要创建包含内容的文件,可以执行以下操作:
new File("C:/Users/ramus/Documents/Jars/test.txt").text = "Test text file content"
这个特殊功能是groovy我最喜欢的部分之一,您甚至可以使用+ =附加到文件中
小智 5
您还可以使用:
def newFile = new File("new.txt")
newFile.write("text to be added to the new file")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14319 次 |
最近记录: |