从Groovy中的byte []逐行读取文件内容 [英] Read File Content Line by Line from byte[] in Groovy

查看:517
本文介绍了从Groovy中的byte []逐行读取文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节[],我需要做的是逐行读取内容。在Java中,我们可以通过将其转换为BufferedReader来获得。但如何使用Groovy Script做到这一点?

I have a byte[], What I need to do is, to read the content line by line. In Java We can get this by converting it to BufferedReader. But how to do it using Groovy Script?

推荐答案

使用 Groovy JDK

Use the power of the Groovy JDK:

new ByteArrayInputStream( arr ).eachLine('UTF-8') { line ->
    println line
}

注意:如果您需要1:1映射在字节和字符之间(例如在处理二进制数据时),使用 iso-8859-1 作为编码。

Note: If you need a 1:1 mapping between bytes and characters (for example when processing binary data), use iso-8859-1 as encoding.

这篇关于从Groovy中的byte []逐行读取文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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