如何将csv字符串转换为字节流 [英] How to convert csv String to bytestream

查看:173
本文介绍了如何将csv字符串转换为字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字符串表示CSV中的行:

I have some strings that represent lines in a CSV:

String headers = "Date,Color,Model,Price";

String line1 = "12-03-2012,Red,Toyota,13500";

我想返回一个字节流,该字节流将与相应的csv文件相对应.我已经看到了如何将csv文件转换为字符串(使用 InputStream BufferedReader ),但是没有反向操作.任何帮助将不胜感激!

I want to return a bytestream that would correspond to the corresponding csv file. I've seen how to convert csv files to strings (using InputStream and BufferedReader), but not the reverse operation. Any help would be appreciated!

推荐答案

您可以使用

You can use String.getBytes(Charset) method for this purpose:

String str = "Lorem ipsum";

byte[] arr = str.getBytes(StandardCharsets.UTF_8);

System.out.println(Arrays.toString(arr));
// [76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109]


另请参阅:
不带'ln'的字符串的System.out.print
将包含多字节字符的字符串拆分为字符串数组

这篇关于如何将csv字符串转换为字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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