node - fs

1, fs module built-in method

1)stat

Detection is a file or directory

fs.stat(fileAddress,(err,stats)=>{

// err error message

//stats.isFile () is the file you something

//stats.isDirectory () is a directory that things do

// returns a Boolean value

})

 

2)mkdir

Create a directory

fs.mkdir(directoryName,mode,callback)

// directoryName: the name of the directory created

// mode: directory access rights, default 0777

// callback: callback, with a parameter error

fs.mkdir('css',(err)=>{...})

You can not establish the same name directory

 

3)writeFile

Write to file

fs.writeFile(...)

parameter list:

Automatic creation does not exist, automatically overwrite same name

 

4)appendFile

Additional file contents

Parameters are the same. 3)

If there is a direct creation, If there is already an additional file directly in the corresponding content

 

5)readFile

Read the file

Parameter List 3) in addition to data

fs.readFile(path,callback(err,data))

Back Buffer format, to convert a string by the method toString

 

6)readdir

Read directory

Parameter List 3) in addition to data

fs.readdir(path,callback(err,data))

All directories and files under read the file

 

7)rename

Rename the file or cut

fs.rename(path,samePathWithNewName,callback(err))

Cut into the specified files and directories:

fs.rename(path,differentPath,callback(err))

 

8)

Remove directory

fs.rmdir()

fs.rmdir(path,cb(err))

You can not delete file

 

9) unlink
to delete files

fs.unlink(path,cb(err))

 

// Note: read files such as asynchronous operation

 

10)createReadStream

So as to read the data file stream

In this way the block is read

// Node read data by way of the file stream 

const FS = the require ( 'FS' )

STR the let = '' // for storing data

// create the object to be read, the parameter file path 
const = readStream fs.createReadStream ( 'a.txt' )

// Each block of data read is completed, a broadcast event data, received through readStream ON 
// read many times when a file is too large 
readStream.on ( 'data', (the chunk) => {
    str += chunk
})

// finished reading. End a broadcast event, by receiving ON 
readStream.on ( 'end', () => {
    console.log(str)
})

// read failure. A broadcast error event, by receiving on 
readStream.on ( 'error', (ERR) => {
    console.log(err)
})

// read not stuck by way of flow

 

11) by createWriteStream

Writing data through the file stream,

Does not directly cover the additional

// writing stream data file by means 

const FS = the require ( 'FS' )
Data const = "This is a data 2 '

const writeStream = fs.createWriteStream('b.txt')

writeStream.write(data, 'utf8')

// mark writing is complete, so as to trigger an event broadcast finish 
writeStream.end ()

writeStream.on('finish', () => {
    console.log('finished')
})

writeStream.on('error', (err) => {
    console.log('error occur!' + err)
})

 

2, the flow duct

The above stream read and write streams can be reproduced by a pipe like water transfer method

 

Guess you like

Origin www.cnblogs.com/Tanqurey/p/11142593.html