1. Read files

fs.readFile(path[,options],callback)

  • Path File path to be read
  • Options The default value of the optional parameter is null
  • Callback callback function
    • Err: error object
    • Data: indicates a specific value
const fs=require('fs')

fs.readFile('./files/1.txt'.'utf8'.(err,data) = >{
    if(err) return console.log(err.message)
    console.log(data)
    // Display conversion
    //console.log(data.toString())
    // Implicit conversion
    //console.log(data+'')
})
Copy the code

2. Write files

fs.writeFile(file,data[,options],callback)

  • File File path to be written
  • Data Indicates the data to be written
  • Options The default value is UTF8
  • Callback callback function
  • Err Error object
const fs=require('fs')

fs.writeFile('./files/2.txt'.'You're the best'.err= >{
    if(err) return console.log(err.message)
    console.log('Write succeeded')})Copy the code

3. Copy files

  • Fs. Used by copyFile (SRC, dest, mode, the callback) v8.5.0 +
  • SRC Indicates the path of the file to be copied
  • Dest Output file path
  • Mode This parameter is optional
  • Callback callback function
  • Err Error object
 const fs = require('fs')
 
  fs.copyFile('./files/1.txt'.'./files/1-copy.txt'.err= > {
    if (err) return console.log(err.message)
    console.log('Copy successful')})Copy the code

4. Path Path processing

const fs = require('fs')
const path = require('path')
console.log(__dirname)//D: qianfeng stage 3 teacher day01 code
// console.log(__filename)//D:\ thousand frontage \ third stage \ teacher \day01\code\04-path Path processing.js

// const abspath = __dirname + '/files/1.txt'
// You are advised to use path.join in the future
// const abspath = path.join(__dirname, './files/1.txt')


// fs.readFile(abspath, 'utf8', (err, data) => {
// if (err) return console.log(err.message)
// console.log(data)
// })



// Web relative path relative file

// Node relative path relative to running environment
Copy the code

Job: After reading the data in TXT, change the format and then write it to another file

const fs = require('fs')
const path = require('path')
const querystring = require('querystring')

fs.readFile(path.join('./1.txt'), 'utf8'.(err, dataStr) = > {
  if (err) return console.log(err.message)
  console.log(dataStr);
  let newArr = []
  let arr = dataStr.trim().split('\n')
  const obj = {}
  // console.log(arr)
  arr.forEach(item= > {
    // console.log(JSON.parse('{"' + item.replace(/ /g, '\",\"').replace(/=/g, '\":\"') + '"}'))
    // newArr.push(JSON.parse('{"' + item.replace(/ /g, '\",\"').replace(/=/g, '\":\"') + '"}'))
    // console.log(querystring.parse(item, ' '))
    newArr.push(querystring.parse(item, ' '))
  })

  obj.message = newArr
  // argument 1 STR
  // Callback can be set to null
  // Parameter 3 sets the data format
  // JSON.stringify
  fs.writeFile(path.join(__dirname, './2.json'), JSON.stringify(obj, null.'\t'), err= > {
    if (err) return console.log(err.message)
    console.log('Data conversion successful')})})Copy the code