Nodejs Basic byte operations, path operations, and text operations

Source: Internet
Author: User

Byte manipulation of the buffer object
    • constructor function
      • The new Buffer (size) parameter is a numeric value that represents the length of the allocated space
      • The new buffer (string,[encoding]) parameter is a string that represents the data stored in buffer, which encodes the optional
      • The new buffer (array) parameter is an array that represents the data stored in buffer
    • Static methods
      • Buffer.isbuffer (obj) Determines if it is buffer (static method is called directly from the class Name)
    • Instance method
      • Buf.write (content,offset,length,encoding);
        • Content indicates what is written to buffer
        • Offset indicates the starting position to write to buffer
        • Length indicates the number of bytes written to buffer
        • Encoding indicates the encoding setting written to buffer (ascii/utf8/utf16le/ucs2/base64/binary/hex)
Path Action Object

Introduce module var path = require ("path");

    1. Path.normalize (path) Canonical path
    2. Path.basename (path[, ext]) Gets the file name, including the extension
    3. Path.dirname (path) Gets the full path of the file, excluding the extension
    4. Path.extname (path) only gets the file name extension
    5. Path.parse (path) Converts a file path in the form of a string into an object form
    6. Path.format (pathobject) Converts the file path in object form to a string form
    7. Path.relative (from, to) finds a relative path to two paths
    8. Path.resolve ([from ...], to) forms the final path after parsing multiple paths
    9. Path.join ([path1][, path2][, ...]) Connection path
    10. Path.sep Path Separator window "\" or Linix "/"
       ‘foo\\bar\\baz‘.split(path.sep) // returns [‘foo‘, ‘bar‘, ‘baz‘]
    11. Path.delimiter environment variable delimiter, in window is ";", Linix system is ":" symbol
File Operation files System object

Introducing the file system module var fs = require (' FS ')

1. Determine if the file exists
    • Fs.exists (path, callback);
      • Callback (exists); Exists==true exists, Exists==false does not exist,
2. View basic information about a file
    • Fs.stat (path, callback);
      • Callback (err, stats);
      • Stats is the information object, with the isfile (), isdirectory () method, and the Size property
3. Open File
    • Fs.open (path, flags[, mode], callback); Callback (err, fd);
      • Path: paths
      • Flags: read-write flag-only "r", reading and writing "r+"
      • Mode: Read/write/execute 4/2/1 usually only used in Linix
      • Callback: callback function (the Second argument is a number that represents the handle to the file);
4. Reading files
    • Asynchronous operation
    • Fs.read (fd, buffer, offset, length, position, callback);
      • FD file Handle
      • Where to read the contents of the buffer file
      • Offset from where to start reading buffer
      • Length reads a few bytes into the buffer
      • Position indicates from where in the file to start reading
      • Callback represents the callback function callback (err, bytesread, buffer)
        • Bytesread the length of bytes read in, equal to
        • Buffer read-in Content
    • Synchronous operation, return value is byte length
    • var ret = Fs.readsync (fd,bf,0,bf.length,0);
5. Writing Files
    • Asynchronous operation
    • Fs.write (fd, buffer, offset, length[, position], Callback)
      • FD file handle, which file to manipulate
      • buffer//string> | buffer> What to write
      • Offset from what position in buffer to start writing
      • Length write a few bytes of content
      • Position indicates where to start writing from in the file
      • Callback = callback function (err, written, buffer)
        • Err indicates an error message
        • Written the byte length of the content that is written
        • Content written by buffer
    • Synchronous operation
    • Fs.writesync (fd, buffer, offset, length[, position]); The return value is the length of the bytes written
6. Close the file
    • Asynchronous Operation Fs.close (fd, callback); Callback (err);
    • Synchronous Operation Fs.closesync (fd); No return value
7. Quick action for reading and writing files
    • Read file Fs.readfile (file[, options], callback); Callback (err, data);
      • The first parameter to read the file
      • If the second parameter passes the encoding, then the data read is the text;
      • If the encoding is not passed, then the data read is buffer
    • Read Synchronous operation, return value Read-to-content
      • Fs.readfilesync (file[, options]);
    • Write files Fs.writefile (file, data[, options], callback); Callback (err);
    • Synchronization operation to write to the file, no return value
      • Fs.writefilesync (file, data[, Options])
8. Folder Operations
    • Create a folder
      • Fs.mkdir (path[, mode], callback); Callback (err);
      • Parameter path to create the folder, and its paths
    • Create a sync operation for a folder with no return value
      • Fs.mkdirsync (path[, Mode])
    • Read folder
      • Fs.readdir (path[, options], callback);
      • Parameter path is the folder directory
      • Options code//string> default ' UTF8 '
      • Callback (err, files); The second parameter is a collection of files, array types, including files and folders
    • Read folder synchronization operation, return value is a collection of contained files, array types, including files and folders
      • Fs.readdirsync (path[, options]);
    • Remove folder, only empty folder Fs.rmdir (path, Callback) can be removed; Callback (err);
    • Remove a sync operation for a folder with no return value Fs.rmdirsync (path)
9. Listening to files and canceling listening operations
    • Changes to the Listening file
      • Fs.watch (filename[, options][, listener]);
        • FileName is the file name to listen on
        • Listener//function> Listener Handler Function. Parameters (event,filename);
        • Event is a change in the file, typically rename and
        • FileName is the file being Monitored.
      • Fs.watchfile (filename[, options], listener);
        • Options has two properties
          • Whether persistent//boolean> is being monitored
          • Interval//integer> is monitored for a time interval of 5007 milliseconds by default
        • listener//function>, Parameters (curr,prev);
          • Curr represents state information before a file changes, prev indicates state information after a file change
    • Cancel Monitoring
      • Fs.unwatchfile (filename[, listener]);
        • listener//function>, which represents the specific function name, can not be anonymous function
10. File Stream Read and write operations

Readstream.pipe (writestream); Piping ideas, Simple notation

* 流文件的读取 fs.createReadStream(path[, options]) * path 读取的文件路径 * options <String> | <Object> 如果是字符串,是设置文件的编码格式 * 返回值 :返回的是读取的文件对象* 流文件的写入 fs.createWriteStream(path[, options]) * path 要写入的文件路径 * options参数含义与读取的一致 * 返回值是写入的文件对象
Note: all file Write operations, if there is no corresponding path to the file, are equivalent to creating a new file

Nodejs Basic byte operations, path operations, and text operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.