---
title: "nodejs 文件系统(fs) 之读写文件"
author: "cjx186"
date: "2014-01-25T23:01:32Z"
tags: []
categories: ["编程语言"]
slug: "nodejs-fs"
draft: false
lastmod: "2014-01-25T23:01:32Z"
id: 2009433
url: "https://xml.iteye.com/blog/2009433"
---

```js
var fs = require("fs");
//  
var data = '写入的的内容'; 
fs.writeFile('f:a.txt', data, 'ascii', function(err){
    if(err){ 
        console.log('写入文件失败'); 
    }else{
        console.log('保存成功');
        fs.readFile('f:a.txt','ascii', function(err, data){ 
            if(err){ 
                console.log('写入文件失败'); 
            }else{ 
				console.log('===读出来了==='+data); 
			} 
		});
 	}
});
//默认情况下，数据编码为utf8；mode=438 ；flag=w
// 在C盘创建一个名为a的文件夹 
fs.mkdir("c:a", function(err){ 
    if(!err){ 
        console.log("操作成功！");  
    }else{ 
        console.log("操作失败！"); 
    } 
});
```