NodeJs์ path, File, Buffer
๊ฒฝ๋ก ๋ค๋ฃจ๊ธฐ
path
๊ฒฝ๋ก ์ ๋ณด
- ํ์ฌ ์คํ ํ์ผ ๊ฒฝ๋ก, ํด๋ ๊ฒฝ๋ก
__filename__dirnamevar path = __dirname + '/image.png';
๊ฒฝ๋ก ๋ค๋ฌ๊ธฐ
path.normalize()..: ๋ถ๋ชจ ํด๋.: ๊ฐ์ ํด๋
๊ฒฝ๋ก ๊ตฌ์ฑ ์์
path.basename(): ํ์ผ ์ด๋ฆ, ๊ฒฝ๋ก ์ค ๋ง์ง๋ง ์์path.dirname(): ํ์ผ์ด ํฌํจ๋ ํด๋ ๊ฒฝ๋กpath.extname(): ํ์ฅ์
๊ฒฝ๋ก ๊ตฌ์ฑ ๊ฐ์ฒด
var info = pathUtil1.parse('/usr/temp/local/image.png');
console.log(info);
{ root: '/',
dir: '/usr/temp/local',
base: 'image.png',
ext: '.png',
name: 'image' }๊ฒฝ๋ก ๋ง๋ค๊ธฐ
pathUtil.sep: ๊ตฌ๋ถ์ /, \pathUtil.join(): ๊ฒฝ๋ก ๋ถ์ด๊ธฐpathUtil.format(): ๊ฒฝ๋ก ๋ง๋ค๊ธฐ
ํ์ผ ์์คํ
ํ์ผ ์์คํ ๋ชจ๋ : fs
์ฃผ์๊ธฐ๋ฅ
- ํ์ผ ์์ฑ/์ฝ๊ธฐ/์ฐ๊ธฐ/์ญ์
- ํ์ผ ์ ๊ทผ์ฑ/์์ฑ
- ๋๋ ํ ๋ฆฌ ์์ฑ/์ฝ๊ธฐ/์ญ์
- ํ์ผ ์คํธ๋ฆผ
- ๋ชจ๋ ํ๋ซํผ์ 100% ํธํ๋์ง ์์
ํน์ง
-
๋น๋๊ธฐ์ ๋๊ธฐ ๋ฐฉ์ ํจ์ ๋ชจ๋ ์ ๊ณต
-
๋น๋๊ธฐ์ : callback โ non-block
-
๋๊ธฐ์ : + Sync โ block ์ฑ๋ฅ์ ์ฃผ์ , ๋ฐํ๊ฐ ์ด์ฉ
-
๋๊ธฐ์
try{
var data = fs.readFileSync('textfile.txt','utf8');
}catch(exception){
console.error('ReadFile Error : ', exception);
}- ๋น๋๊ธฐ์
fs.readFile('textfile.txt','utf8',function(error,data){
if(err){
console.error('ReadFile Error : ', exception);
}else{
//์ ์์ฒ๋ฆฌ
}
});ํ์ผ ์์คํ ๋ค๋ฃจ๊ธฐ
ํ์ผ ๋ค๋ฃจ๊ธฐ
ํ์ผ ๋ค๋ฃจ๊ธฐ
- File descriptor
- File Path
FileDescription๋ก ํ์ผ ๋ค๋ฃจ๊ธฐ
fs.read(fd, buffer, offset, length, position, callback);fs.readSync(fd, buffer, offset, length, position);
ํ์ผ ๊ฒฝ๋ก๋ก ํ์ผ ๋ค๋ฃจ๊ธฐ
fs.readFile(filename, option, callback);fs.readFileSync(filename, option);
FileDescription ์ป๊ธฐ : Open ํจ์
var fd = fs.openSync(path, flags)fs.open(path,flags,function(err,fd){});
flag
r: readw: writea: add
File Close
fs.close(fd, callback)fs.closeSync(fd)
ํ์ผ ์ข ๋ฅ
- ๋ฌธ์์ด ์ฝ๊ธฐ : ์ธ์ฝ๋ฉ
- ๋ฐ์ด๋๋ฆฌ ์ฝ๊ธฐ : buffer
- ์ธ์ฝ๋ฉ ์ค์ ์ํ๋ฉด buffer๋ก ์ฝ๋๋ค.
ํ์ผ ์ฝ๊ธฐ ์์ - ํ์ผ ๋์คํฌ๋ฆฝํฐ, ๋๊ธฐ์
var fd = fs.openSync(file, 'r'); // Get File descriptor
var buffer = new Buffer(10); // Make Buffer
var byte = fs.readSync(fd, buffer, 0, buffer.length, 0); // Read File
console.log('File Contenst : ', buffer.toString('utf-8')); // Set Encoding
fs.closeSync(fd); // File Closeํ์ผ ์ฝ๊ธฐ ์์ - ํ์ผ ๋์คํฌ๋ฆฝํฐ, ๋น๋๊ธฐ
fs.open(file,'r',function(err,fd2){
var buffer2 = new Buffer(20);
fs.read(fd2, buffer2, 0, buffer2.length, 10, function(err,byteRead,buffer){
console.log('File Read ', byteRead, 'bytes');
console.log('File Content : ', buffer.toString('utf-8'));
fs.close(fd,function(err){});
})
})ํ์ผ ์ฝ๊ธฐ - ๋๊ธฐ์
var data = fs.readFileSync(file, 'utf-8');var imageData = fs.readFileSync('./image.jpg');
ํ์ผ ์ฝ๊ธฐ - ๋น๋๊ธฐ, ์ธ์ฝ๋ฉ
fs.readFile(file, 'UTF-8', function (err,name) {
});ํ์ผ ์ํ - ์กด์ฌ ํ์ธ
ํ์ผ ์กด์ฌ ํ์ธํ๊ธฐ
fs.access(Sync)fs.stat(Sync)
ํ์ผ ์ ๊ทผ ๊ฐ๋ฅ ํ์ธํ๊ธฐ
fs.access(path, callback)fs.accessSync(path, model)
์ ๊ทผ ๋ชจ๋
fs.F_OK: ์กด์ฌ ํ์ธfs.R_OK, W_OK, X_OK: ์ฝ๊ธฐ / ์ฐ๊ธฐ / ์คํ ์ฌ๋ถ ํ์ธ
ํ์ผ ์ ๊ทผ ์ฌ๋ถ ํ์ธ ํ ์ฝ๊ธฐ - ๋๊ธฐ
try{
fs.accessSync(file,fs.F_OK);
}catch(exception){
//ํ์ผ ์์
}ํ์ผ ์ ๊ทผ ์ฌ๋ถ ํ์ธ ํ ์ฝ๊ธฐ - ๋น๋๊ธฐ
fs.access(file, fs.F_OK | fs.R_OK, function(err){
if(err){
//์๋ฌ ์ฒ๋ฆฌ
}
fs.readFile(file, 'utf8',function(err,data){
if(err){
//์๋ฌ ์ฒ๋ฆฌ
}
})
})ํ์ผ ์ํ
ํ์ผ ์ํ ์ป๊ธฐ
fs.stat(path, callback)fs.statSync(path)stats.isFile(),stats.isDirectory(): ํ์ผ, ๋๋ ํ ๋ฆฌ ์ฌ๋ถstats.size: ํ์ผ ํฌ๊ธฐstats.birthtime: ์์ฑ์ผstats.atime: ์ ๊ทผ ์๊ฐstats.mtime: ์์ ์ผ
ํ์ผ ์ํ ํ์ธ : ๋๊ธฐ
try{
var stats = fs.statSync(file);
}catch(err){
console.err('Cannot Access File', err);
}ํ์ผ ์ํ ํ์ธ : ๋น๋๊ธฐ
fs.stat(file, function(err,stats){
if(err){
console.err('Cannot Access File', err);
}
})ํ์ผ์ ์ ์ฅ
fs.write(fd, data, callback)fs.writeFile(filename,data,callback)fs.writeFileSync(filename,data)fd,filename: ํ์ผ ๋์คํฌ๋ฆฝํฐ, ํ์ผ ๊ฒฝ๋กfd,data: ๋ฌธ์์ด ํน์ BufferEncoding: ๋ฌธ์์ด ์ ์ฅ ์ ์ธ์ฝ๋ฉ- ๊ฐ์ ํ์ผ ์ด๋ฆ โ ๋ฎ์ด์ฐ๊ธฐ
ํ์ผ์ ๋ด์ฉ ์ถ๊ฐ
fs.appendFile(file,data,callback)fs.appendFileSync(file,data)
ํ์ผ ์ญ์
fs.unlink(path, callback)fs.unlinkSync(path)
ํ์ผ ์ด๋ฆ ๋ณ๊ฒฝ/์ด๋
fs.rename(oldPath,newPath,callback)fs.renameSync(oldPath,newPath,callback)
๋๋ ํ ๋ฆฌ ์์ฑ
fs.mkdir(path,callback)fs.mkdirSync(path,callback)- ๊ฐ์ ์ด๋ฆ์ ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด ์คํจ
๋๋ ํ ๋ฆฌ ์ญ์
fs.rmdir(path,callback)fs.rmdirSync(path)- ๋๋ ํ ๋ฆฌ๊ฐ ๋น์ด์์ง ์์ผ๋ฉด ์คํจ
๋๋ ํ ๋ฆฌ ๋ด ํ์ผ ๋ชฉ๋ก
fs.readdir(path, callback)fs.readdirSync(path)- ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด ์๋ฌ
ํ์ผ ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
fs.createReadStream(path)fs.createWriteStream(path)
๋ฒํผ
๋ฒํผ ์ป๊ธฐ
ํ์ผ์์ ์ฝ๊ธฐ
var fileBuffer = fs.readFileSync('image.jpg');
๋คํธ์ํฌ์์ ์ป๊ธฐ
socket.on('data',function(data){})
๋ฒํผ ๋ง๋ค๊ธฐ
new Buffer(size)new Buffer(array)new Buffer(str)- ์ฌ์ด์ฆ๋ ๊ณ ์ ์
๋ชจ๋ ํจ์
Buffer.byteLength(String,encoding): ๋ฐ์ดํธ ๊ธธ์ดBuffer.compare(buf1,buf2): ๋น๊ตBuffer.concat(list, totallength): ๋ถ์ด๊ธฐBuffer.isBuffer(obj): ๋ฒํผ ํ์ธBuffer.isEncoding(encoding): ์ธ์ฝ๋ฉ
๊ฐ์ฒด ๋ฉ์๋
buffer.lengthbuf.fill(value)buf.slice(start,end)buf.copy()