2011-04-26 107 views

回答

43

這裏是fs.read()fs.open()返回的文件描述符-ing前100個字節的例子:

var fs = require('fs'); 

fs.open('file.txt', 'r', function(status, fd) { 
    if (status) { 
     console.log(status.message); 
     return; 
    } 
    var buffer = new Buffer(100); 
    fs.read(fd, buffer, 0, 100, 0, function(err, num) { 
     console.log(buffer.toString('utf8', 0, num)); 
    }); 
}); 
+0

@AlexMA我已經編輯了答案,以反映變化的,謝謝! – 2015-02-03 20:29:32

+2

其utf8而不是utf-8 http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end – WebFreak001 2015-02-22 12:18:14