Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Delete specific rows from a text file in NodeJS

I am trying to delete a specific row from a text file using NodeJs, but I am having trouble to do so. Can anyone please help me?

This is the content inside the text file and I am trying to delete example3:example3 from the text file.

example1:example1
example2:example2
example3:example3
example4:example4

This is the code that should work but doesn't

const fs = require('fs')
const path = require('path')

const filePath = path.join(__dirname, '../data/test.txt')
const fileData = fs.readFileSync(filePath, 'utf-8').split('\r\n')

const position = 3

fileData.splice(position - 1, 1)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to save the file with fs.writeFile or fs.writeFileSync

Synchronous:

const fs = require('fs')
const path = require('path')
const filePath = '../data/test.txt'

const filePath = path.join(__dirname, filePath)
const fileData = fs.readFileSync(filePath, 'utf-8').split('\r\n')

const position = 3

fileData.splice(position - 1, 1)
const dataString = fileData.join('\r\n')
fs.writeFileSync(filePath, dataString)

Asynchronous:

const fs = require('fs')
const path = require('path')
const filePath = '../data/test.txt'

const filePath = path.join(__dirname, filePath )

fs.readFile(filePath, (err, data) => {
  if (err) throw err

  const fileData a data.split('\r\n')

  const position = 3

  fileData.splice(position - 1, 1)
  const dataString = fileData.join('\r\n')

  fs.writeFile(filePath, dataString, (err) => {
    if (err) throw err
    else console.log('file saved')
  })
})


about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda