Giter Site home page Giter Site logo

notetest's Introduction

生活不止眼前的枸杞~

notetest's People

Contributors

frontendx avatar

Watchers

 avatar

notetest's Issues

fs 模块

fs 模块

导语

fs是filesystem的缩写,该模块提供本地文件的读写能力,基本上是POSIX文件操作命令的简单包装。但是,这个模块几乎对所有操作提供异步和同步两种操作方式,供开发者选择。

目录

  1. readFileSync()
  2. writeFileSync()
  3. exists(path, callback)
  4. mkdir(),writeFile(),readfile()
  5. mkdirSync(),writeFileSync(),readFileSync()
  6. readdir()
  7. stat()
  8. watchfile(),unwatchfile()
  9. createReadStream()
  10. createWriteStream()

1、readFileSync()

readFileSync方法用于同步读取文件,返回一个字符串。

var text = fs.readFileSync(fileName, "utf8");

// 将文件按行拆成数组
text.split(/\r?\n/).forEach(function (line) {
    // ...
});     

该方法的第一个参数是文件路径,第二个参数是文本文件编码,默认为utf8。

不同系统的行结尾字符不同,可以用下面的方法判断。

// 方法一,查询现有的行结尾字符
var EOL = fileContents.indexOf("\r\n") >= 0 ? "\r\n" : "\n";

// 方法二,根据当前系统处理
var EOL = (process.platform === 'win32' ? '\r\n' : '\n')

2、writeFileSync()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.