-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
4,490 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var fs = require( 'fs' ); | ||
var stat = fs.stat; | ||
|
||
/* | ||
* 复制目录中的所有文件包括子目录 | ||
* @param{ String } 需要复制的目录 | ||
* @param{ String } 复制到指定的目录 | ||
*/ | ||
var copy = function( src, dst ){ | ||
// 读取目录中的所有文件/目录 | ||
fs.readdir( src, function( err, paths ){ | ||
if( err ){ | ||
throw err; | ||
} | ||
paths.forEach(function( path ){ | ||
var _src = src + '/' + path, | ||
_dst = dst + '/' + path, | ||
readable, writable; | ||
stat( _src, function( err, st ){ | ||
if( err ){ | ||
throw err; | ||
} | ||
// 判断是否为文件 | ||
if( st.isFile() ){ | ||
// 创建读取流 | ||
readable = fs.createReadStream( _src ); | ||
// 创建写入流 | ||
writable = fs.createWriteStream( _dst ); | ||
// 通过管道来传输流 | ||
readable.pipe( writable ); | ||
} | ||
// 如果是目录则递归调用自身 | ||
else if( st.isDirectory() ){ | ||
exists( _src, _dst, copy ); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
// 在复制目录前需要判断该目录是否存在,不存在需要先创建目录 | ||
var exists = function( src, dst, callback ){ | ||
fs.exists( dst, function( exists ){ | ||
// 已存在 | ||
if( exists ){ | ||
callback( src, dst ); | ||
} | ||
// 不存在 | ||
else{ | ||
fs.mkdir( dst, function(){ | ||
callback( src, dst ); | ||
}); | ||
} | ||
}); | ||
}; | ||
// 复制目录 | ||
exists( './src', './build', copy ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var fs = require('fs') | ||
|
||
/** | ||
* @brief walk dir path | ||
* | ||
* @param path 路径 | ||
* @param floor 层数 | ||
* @param handleFile 文件、文件夹处理函数 | ||
* | ||
* @return | ||
*/ | ||
function walk(path, floor, handleFile) { | ||
handleFile(path, floor); | ||
floor ++; | ||
fs.readdir(path, function(err, files) { | ||
if(err) { | ||
console.log('read dir error') | ||
} else { | ||
files.forEach(function(item){ | ||
var tmpPath = path + '/' + item; | ||
fs.stat(tmpPath, function(err1, stats) { | ||
if(err1) { | ||
console.log('stat error') | ||
} else { | ||
if(stats.isDirectory()) { | ||
walk(tmpPath, floor, handleFile); | ||
} else { | ||
handleFile(tmpPath, floor); | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
exports.walk = walk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var fs = require('fs'); | ||
var dirWalk = require('./dirWalk'); | ||
|
||
function handleFile(path, floor) { | ||
var blankStr = ''; | ||
for(var i = 0; i < floor; i ++) { | ||
blankStr += ' '; | ||
} | ||
|
||
fs.stat(path, function(err, stats) { | ||
if(err) { | ||
console.log('stat error'); | ||
} else { | ||
if(stats.isDirectory()) { | ||
console.log('+ ' + blankStr + path); | ||
} else { | ||
console.log('- ' + blankStr + path); | ||
} | ||
} | ||
}) | ||
} | ||
|
||
|
||
dirWalk.walk('../class-file', 0, handleFile); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
中国 | ||
|
||
我是一个中国人 Hello myself! | ||
����һ���й��� Hello myself! | ||
����һ���й��� Hello myself! | ||
����һ���й��� Hello myself! | ||
����һ���й��� Hello myself! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
我是一个中国人 Hello myself! | ||
我是一个中国人 Hello myself! | ||
我是一个中国人 Hello myself! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// NodeJS file read write | ||
// | ||
// 由于Node.js仅支持如下编码:utf8, ucs2, ascii, binary, base64, hex,并不支持中文GBK或GB2312之类的编码, | ||
// 因此如果要读写GBK或GB2312格式的文件的中文内容,必须要用额外的模块:iconv-lite | ||
// 安装模块:npm install iconv-lite | ||
|
||
var fs = require('fs') | ||
var iconv = require('iconv-lite') | ||
|
||
var file = './demo.txt' | ||
var fileutf8 = './demo_utf8.txt' | ||
|
||
console.log("GBK 编码\n") | ||
writeFile(file) | ||
readFile(file) | ||
|
||
//++++++++++++++++++++++++++++++++++++++++// | ||
|
||
console.log("UTF8 编码\n") | ||
writeFileUtf8(fileutf8) | ||
readFileUtf8(fileutf8) | ||
|
||
/** | ||
* 将字符串内容写入文件 [GBK] | ||
* | ||
* @brief writeFile | ||
* | ||
* @param file | ||
* | ||
* @return | ||
*/ | ||
function writeFile(file){ | ||
// 测试用的中文 | ||
var str = "\n 我是一个中国人 Hello myself!" | ||
// 把中文转为字节数组 | ||
var arr = iconv.encode(str, 'gbk') | ||
console.log(arr) | ||
|
||
// appendFile 如果文件不存在,会自动创建新文件 | ||
// 如果用writeFile,那么会删除旧文件,直接写新文件 | ||
fs.appendFile(file, arr, function(err){ | ||
if(err) | ||
console.log('Error : ' + err) | ||
else | ||
console.log('Success!') | ||
}) | ||
} | ||
|
||
/** | ||
* 读取文件内容 | ||
* | ||
* @brief readFile | ||
* @param file | ||
* @param encode | ||
* | ||
* @return | ||
*/ | ||
function readFile(file){ | ||
fs.readFile(file, function(err, data){ | ||
if(err) | ||
console.log('Read file error!') | ||
else{ | ||
// 读取成功时,输出字节数组 | ||
console.log(data) | ||
// 把数组转为gbk中文 | ||
var str = iconv.decode(data, 'gbk') | ||
console.log(str) | ||
} | ||
}) | ||
} | ||
|
||
|
||
//++++++++++++++++++++++++++++++++++++++++// | ||
|
||
|
||
function writeFileUtf8(file){ | ||
// 测试用的中文 | ||
var str = "\n 我是一个中国人 Hello myself!" | ||
|
||
// appendFile 如果文件不存在,会自动创建新文件 | ||
// 如果用writeFile,那么会删除旧文件,直接写新文件 | ||
fs.appendFile(file, str, function(err){ | ||
if(err) | ||
console.log('Error : ' + err) | ||
else | ||
console.log('Success!') | ||
}) | ||
} | ||
|
||
function readFileUtf8(file){ | ||
fs.readFile(file, function(err, data){ | ||
if(err) | ||
console.log('Read file error!') | ||
else{ | ||
// 读取成功时,输出字节数组 | ||
console.log(data) | ||
// 把数组转为gbk中文dd | ||
var str = iconv.decode(data, 'utf8') | ||
console.log(str) | ||
} | ||
}) | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.