[英]node.js readFile with size
in php i use 在我使用的PHP中
$filename = "a.txt";
$handle = fopen($filename, "r");
$heading = fread($handle, 4);
fclose($handle);
print $heading;
a.txt content a.txt内容
hello every one
大家好
and the output its 并输出它
hell
地狱
now in node.js i use 现在在我使用的node.js
fs = require('fs');
fs.readFile('a.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
its output 它的输出
hello every one
大家好
how can i use size length on node.js ? 我如何在node.js上使用大小长度?
Here is a basic example: 这是一个基本的例子:
var fs = require("fs");
var fileName = "a.txt";
fs.open(fileName, "r", function(error, fd) {
var buffer = new Buffer(4);
fs.read(fd, buffer, 0, 4, null, function(error, bytesRead, buffer) {
var data = buffer.toString("ascii", 0, buffer.length);
console.log(data);
fs.close(fd);
});
});
You should be able to use something like this: http://www.w3schools.com/jsref/jsref_substring.asp 你应该可以使用这样的东西: http : //www.w3schools.com/jsref/jsref_substring.asp
with: data.substring(0,4);
with:
data.substring(0,4);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.