nodejs 파일이 있는지 체크하는 방법(is_file)
● 파일이 있는지 체크 하는 방법
var is_file = 0;
fs.stat(data_file, (err,stats) => {
if ( stats.isFile() ){
is_file = 1;
console.log('stats.isFile(): ' + stats.isFile());
}
});
console.log('is_file:' + is_file + "\n");
// 함수로 만들어 사용
/**
* 파일이 있나
*
* @param string filepath
*
* @return bool
*
*/
function isFileSync(filepath) {
let flag = true;
try{
fs.accessSync(filepath, fs.constants.F_OK);
}catch(e){
flag = false;
}
return flag;
}
더 간단한 방식 - Sync가 붙어 있네요. 동기식 이란 의미 입니다.
const is_file = fs.existsSync(data_file);