여씨의 개발이야기

[Node.js] fs를 이용해 .txt파일 내용 가져오기 본문

🐾 Programming Lang/🎈 Node.js

[Node.js] fs를 이용해 .txt파일 내용 가져오기

yeossi 2022. 2. 21. 00:16

텍스트 파일 읽기

const fs = require('fs');

const text = fs.readFileSync("source.txt");
const textSplitArr = text.toString().split('\n');//띄어쓰기로 나누고 싶을 때는

텍스트 파일 쓰기

const fs = require('fs');

const text = 'UTF-8로 저장될 텍스트';
fs.writeFileSync("target.txt", '\ufeff' + text, {encoding: 'utf8'});

참고로 Node.js는 파일을 저장할 때 기본적으로 BOM 정보를 저장하지 않기 때문에 \ufeff를 텍스트에 앞에 명시해줘야 함.

 

Comments