I'm trying to read txt file and pass data from it using Sockets, but looks like I'm missing newlines while writing to outputstream.
My txt file is:
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
}
}
ByteArrayOutputStream:
{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": {% "streetAddress": "21 2nd Street", "city": "New York", "state": "NY",
"postalCode": "10021-3100" }}
private byte[] readFile(String path) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
File file = new File(path);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()){
out.writeUTF(scanner.nextLine());
}
byte[] bytes = baos.toByteArray();
return bytes;
}
EDIT:
System.getProperty("line.separator");
Helped me with new lines, but I still get some invalid characters in baos
System.out.println(new String(baos.toByteArray()));