如何将文本追加到新文件中 [英] How to append text to a file on a new line

查看:89
本文介绍了如何将文本追加到新文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的Groovy代码不会在新行上追加 hello again

Why does the following Groovy code not append the "hello again" on a new line?

f = new File('myfile.txt')
f.append('hello again!\n')

这会在最后一行后添加 myfile.txt ,但不会添加到新行中。如何将文本追加到新行上?

This adds to myfile.txt after the last line but not on a new line. How do I append the text on a new line?

推荐答案

您应该将 \\\
第一。这样你的换行符就会写在你的文本之前:

You should put the \n first. That way your newline will be written before your text:

f.append('\nhello again!')

这篇关于如何将文本追加到新文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆