Skip to content

Commit

Permalink
add file path ctl
Browse files Browse the repository at this point in the history
  • Loading branch information
kugouming committed Jan 25, 2016
1 parent 2c1904f commit b8f7a79
Show file tree
Hide file tree
Showing 33 changed files with 4,490 additions and 0 deletions.
56 changes: 56 additions & 0 deletions nodejs/class-file/copyfile.js
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 );
37 changes: 37 additions & 0 deletions nodejs/class-file/dir/dirWalk.js
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;
24 changes: 24 additions & 0 deletions nodejs/class-file/dir/walk_index.js
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);
7 changes: 7 additions & 0 deletions nodejs/class-file/file/demo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
中国

我是一个中国人 Hello myself!
����һ���й��� Hello myself!
����һ���й��� Hello myself!
����һ���й��� Hello myself!
����һ���й��� Hello myself!
4 changes: 4 additions & 0 deletions nodejs/class-file/file/demo_utf8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

我是一个中国人 Hello myself!
我是一个中国人 Hello myself!
我是一个中国人 Hello myself!
103 changes: 103 additions & 0 deletions nodejs/class-file/file/file.js
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)
}
})
}

6 changes: 6 additions & 0 deletions nodejs/class-file/node_modules/iconv-lite/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions nodejs/class-file/node_modules/iconv-lite/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions nodejs/class-file/node_modules/iconv-lite/Changelog.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions nodejs/class-file/node_modules/iconv-lite/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b8f7a79

Please sign in to comment.