【发布时间】:2021-10-24 06:52:51
【问题描述】:
所以,我几乎有以下代码来使用 ProcessBuilder 将一些文本写入文件:
val requestStringBody = "{\"transfers\": [{\"amount\": 100, \"name\": \"Steve Rogers\", \"taxId\": \"330.731.970-10\",\"bankCode\": \"001\",\"branchCode\": \"1234\",\"accountNumber\": \"123456-0\"}]}"
ProcessBuilder("cmd", "/c", ".\\git-bash.lnk", "-c", "echo -n \"$requestStringBody\" >> message.txt")
.directory(File(My Path))
.start()
.waitFor()
由于一些 Windows 问题,我不得不使用 git-bash.lnk。
我尝试了使用不同变量作为 requestBodyString 的相同命令,它工作正常,这让我相信该字符串有问题,但我就是不知道是什么。
编辑:使用 File(fileName).writeText(fileContent) 解决它。
【问题讨论】:
-
您可以使用Files.writeString 将文本写入文件。
-
@VGR 我用的是jdk8,所以writeString被认为是未解析的引用
-
然后使用Files.write。例如,
Files.write(Paths.get(myPath), Arrays.asList(requestStringBody));.