Reading file as bytes without newline or carriage return

Viewed 1159

I want to read an entire file into a byte array without newline or carriage return. I get 13,10 also in the byte array. Is there a way to read the entire file without newline or carriage return. I have used the below code:

InputStream in = new FileInputStream(file);
numBytesRead=in.read(result, offset, noBytes);

Is there any other way?

1 Answers

As far as I know, you'll have to filter that yourself:

byte[] raw = Files.readAllBytes(file.toPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (byte b : raw)
    if (b != 10 && b != 13)
        baos.write(b);
byte[] result = baos.toByteArray();
Related