Notice: file_put_contents(): Write of 4960 bytes failed with errno=28 No space left on device in /var/www/tgoop/post.php on line 50

Warning: file_put_contents(): Only 12288 of 17248 bytes written, possibly out of free disk space in /var/www/tgoop/post.php on line 50
Node.JS [ru] | Серверный JavaScript@we_use_js P.4245
WE_USE_JS Telegram 4245
👩‍💻 Чтение и обработка файла с использованием потоков

Напишите функцию, которая принимает путь к файлу и возвращает Promise. Функция должна читать файл построчно с использованием потоков (streams), подсчитывать количество строк и возвращать объект с количеством строк и содержанием первой строки.

Пример использования:

const filePath = "./example.txt";

processFile(filePath)
.then(result => {
console.log(result);
// Ожидаемый результат (пример):
// { linesCount: 10, firstLine: "This is the first line of the file." }
})
.catch(error => {
console.error("Error:", error);
});


Решение задачи🔽

const fs = require("fs");
const readline = require("readline");

function processFile(filePath) {
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(filePath, { encoding: "utf8" });
const rl = readline.createInterface({ input: stream });

let linesCount = 0;
let firstLine = null;

rl.on("line", line => {
if (linesCount === 0) {
firstLine = line;
}
linesCount++;
});

rl.on("close", () => {
resolve({ linesCount, firstLine });
});

rl.on("error", error => {
reject(error);
});
});
}
Please open Telegram to view this post
VIEW IN TELEGRAM
👍6



tgoop.com/we_use_js/4245
Create:
Last Update:

👩‍💻 Чтение и обработка файла с использованием потоков

Напишите функцию, которая принимает путь к файлу и возвращает Promise. Функция должна читать файл построчно с использованием потоков (streams), подсчитывать количество строк и возвращать объект с количеством строк и содержанием первой строки.

Пример использования:

const filePath = "./example.txt";

processFile(filePath)
.then(result => {
console.log(result);
// Ожидаемый результат (пример):
// { linesCount: 10, firstLine: "This is the first line of the file." }
})
.catch(error => {
console.error("Error:", error);
});


Решение задачи🔽

const fs = require("fs");
const readline = require("readline");

function processFile(filePath) {
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(filePath, { encoding: "utf8" });
const rl = readline.createInterface({ input: stream });

let linesCount = 0;
let firstLine = null;

rl.on("line", line => {
if (linesCount === 0) {
firstLine = line;
}
linesCount++;
});

rl.on("close", () => {
resolve({ linesCount, firstLine });
});

rl.on("error", error => {
reject(error);
});
});
}

BY Node.JS [ru] | Серверный JavaScript


Share with your friend now:
tgoop.com/we_use_js/4245

View MORE
Open in Telegram


Telegram News

Date: |

In handing down the sentence yesterday, deputy judge Peter Hui Shiu-keung of the district court said that even if Ng did not post the messages, he cannot shirk responsibility as the owner and administrator of such a big group for allowing these messages that incite illegal behaviors to exist. The Channel name and bio must be no more than 255 characters long How to Create a Private or Public Channel on Telegram? Your posting frequency depends on the topic of your channel. If you have a news channel, it’s OK to publish new content every day (or even every hour). For other industries, stick with 2-3 large posts a week. How to create a business channel on Telegram? (Tutorial)
from us


Telegram Node.JS [ru] | Серверный JavaScript
FROM American