OSD600 LAB 1

Researching the Node.js to research

Read and write operations in FS
Basic read and write operations, buffer binary operations, and stream operations are provided in the file module.

Basic read and write operations
When the WIRTE() read adverbial clause () method provides read and write operations, you need to execute fs.open (path, flag, [mode], [callback (ERR, FD)]) to open the file first. Where path is the path of the file, marked as open, can be R (read), W (write) (created if the file does not exist), W + (open the file in read-write mode, if the file does not exist, then Create), r + (open file in read-write mode), one (open file in append mode, create if file does not exist), a + (open file in read append mode, create if file does not exist .

Fs.read(fd,buffer,offset,length,position,callback): Its function is to read data from the specified file descriptor fd and write to the buffer object pointed to by the buffer. Offset is the write offset of the buffer. The quantity .length is the number of bytes to read from the file. The starting position of the location file read. If the value of the location is null, it will be read from the location of the current file pointer. The callback function passes bytesRead and buffer, indicating the number of bytes read and the buffer object, respectively.

Fs.write (FD, buffer, offset, length [, position], callback): The function is to write the buffer content to the FD.

fs.readFile (file [, option], callback): read the file where the file can be the file name or file path, the options can be objects or strings including the encoding when reading the file, the way the file is read (default is ‘ R’);
fs.writeFile (file, data [, option], callback): write to the file. The data is the content that can be written as an optional parameter, which can be encoding (encoding mode), mode (permission) and flag (the way the file is written, the default is ‘W’)
fs.appendFile (file, data [, option], callback) add content at the end of the file

To read a file through the READFILE and WriteFile implementations, and write the file to another file.

fs.readFile('test.txt',function(err,data){
    if(err){
        console.log(err)
    }
    fs.writeFile('test1.txt',data,0,'utf8',function(err){
        if(err){
            throw err;
        }
    })
})