Bun: Process file line by line

In NodeJs I can use fs and readline to make an interface and read the file line by line this is usefull for large text files (for me 100GB +) how can I achieve similar thing using bun

I've tried to read the file like this:

const input = Bun.file("input.txt"); 

but it's the same as fs.readFile which is not what I want in this case.


Nodejs example

const readline = require("readline"); const fs = require("fs"); const input = fs.createReadStream("file.txt", { encoding: "utf16le", }); const rl = readline.createInterface({ input }); rl.on("line", (line) => { // proccess line }) 
1

1 Answer

As @GrafiCode mentioned in the comments

const foo = Bun.file("foo.txt");

does not read the file from the disk it creates BunFile which you can read in many ways one of them is stream

await foo.stream(); // contents as ReadableStream

you can read more here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like