码迷,mamicode.com
首页 > 其他好文 > 详细

Node和Electron开发入门(四):操作PC端文件系统

时间:2017-09-11 18:02:48      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:路径   span   atom   regexp   更改   成功   bsp   const   技术   

一、调用PC端默认方式打开本地文件
在main.js里

 // 打开系统本地文件或者网页链接
  const {shell} = require(‘electron‘);
  // Open a local file in the default app
  var path1 = "d:\\ProjectsSpace\\ElectronProjects\\ElectronTest2\\app\\html\\config\\record.txt";
  shell.openItem(path1);

  // Open a URL in the default way
  shell.openExternal(‘https://github.com‘);

 

这个使用的是electron里shell模块的方法,只能打开和删除,不能通过代码更改文件内容,具体请看shell的api http://electron.atom.io/docs/api/shell/

二、通过代码读取、写入、更名、删除、遍历目录、链接等 POSIX 文件系统操作
这个使用的是node.js里的fs 模块,在electron中使用,案例代码如下:
在main.js里

//本地文件写入
  var path = require(‘path‘);
  var _path = path.join(__dirname, ‘..‘, ‘\\app\\html\\config\\record.txt‘);
  var path1 = "d:\\ProjectsSpace\\ElectronProjects\\ElectronTest2\\app\\html\\config\\record.txt";
  console.log(_path, path1);//测试路径对不对的
  var fs = require(‘fs‘);
  fs.readFile(_path, ‘utf8‘, function (err, data) {
    if (err) return console.log(err);
  });

  fs.writeFile(_path, "electron + Javascript", function (err) {
    if (!err)
      console.log("写入成功!")
  })

 

关于fs更详细的使用方法,请看相关api,或者下图

技术分享

Node和Electron开发入门(四):操作PC端文件系统

标签:路径   span   atom   regexp   更改   成功   bsp   const   技术   

原文地址:http://www.cnblogs.com/sxz2008/p/7505622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!