3

When I want read bytes from a file in Java, I can do like this:

InputStream is = new FileInputStream(...);
while((int b = is.read()) != -1){
    //...analysis the bytes.
}

And how do it in Haskell? I don't see any function witch can read bytes module System.IO and Data.ByteString.

1

1 Answer 1

2

Use readFile

import Data.ByteString(readFile)

main = do 
    content <-  Data.ByteString.readFile "path/to/file"
    print content

It read the file and print it content on screen.

Sign up to request clarification or add additional context in comments.

5 Comments

It read ByteString which is in fact array of Word8 which is smart way to name byte.
So you mean I don't need to import ByteString to read Word8 which like Byte in Java ?
Easiest way to check that is to try, but yes, you don't need to import it.
@talex the readFile in prelude reads into a String. It appears the question asker wants to read into bytes rather then characters. I think ByteString is the correct thing for that.
@Potato44 then proper (Data.ByteString) function should be imported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.