1

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!

Anthony
  • 311
  • 1
  • 3
  • 13

1 Answers1

0

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]

See also:
System.out.print of string without 'ln'
Splitting a string containing multi-byte characters into an array of strings