How to convert csv String to bytestream

家住魔仙堡 提交于 2021-01-01 07:54:20

问题


I have some strings that represent lines in a CSV:

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

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

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!


回答1:


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]


来源:https://stackoverflow.com/questions/65327530/how-to-convert-csv-string-to-bytestream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!