B
B
bezvozni2018-10-01 20:00:12
Node.js
bezvozni, 2018-10-01 20:00:12

How to take the first line from txt, assign it to a variable, delete it and put it at the end of the same txt (Node.js)?

'fs' module. Async is not needed. Judging by the search, the most difficult thing is to delete the line.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uiworks, 2018-10-01
@bezvozni

const fs = require("fs");

fs.readFile('file.txt', (err, data) => {
    if (err) {
        throw err;
    }

    let fileRows = data.toString().split('\n');

    let firstRow = fileRows.shift();

    fileRows.push(firstRow);

    const fileData = new Uint8Array(Buffer.from(fileRows.join('\n')));

    fs.writeFile('file.txt', fileData, (err) => {
        if (err) {
            throw err;
        }

    });

});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question