如何将 Groovy 中的文件读入字符串? [英] How to read a file in Groovy into a string?

查看:21
本文介绍了如何将 Groovy 中的文件读入字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文件系统中读取一个文件并将整个内容加载到 groovy 控制器中的字符串中,最简单的方法是什么?

I need to read a file from the file system and load the entire contents into a string in a groovy controller, what's the easiest way to do that?

推荐答案

String fileContents = new File('/path/to/file').text

如果您需要指定字符编码,请改用以下内容:

If you need to specify the character encoding, use the following instead:

String fileContents = new File('/path/to/file').getText('UTF-8')

这篇关于如何将 Groovy 中的文件读入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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