javascript - 如何在 fs.readFileSync 中使用通配符
问题描述
How do ya make is so 而不是只读取当前文件夹中的 javalin.datasheet ,而是读取所有 *.datasheet 并将它们转换为
我尝试使用 const data = fs.readFileSync('*.datasheet');
但这不起作用,第一次使用 node.js 文件.. 尝试学习一些基础知识。
const fs = require('fs');
const data = fs.readFileSync('javalin.datasheet');
const amountOfColumnsOffset = 0x44;
const amountOfRowsOffset = 0x48;
const headersOffset = 0x5c;
const amountOfBytesInHeader = 12;
const amountOfBytesInCell = 8;
const amountOfColumns = data.readInt32LE(amountOfColumnsOffset);
const amountOfRows = data.readInt32LE(amountOfRowsOffset);
const cellsOffset = headersOffset + amountOfColumns * amountOfBytesInHeader;
const amountOfBytesInRow = amountOfBytesInCell * amountOfColumns;
const stringsOffset = cellsOffset + amountOfRows * amountOfColumns * amountOfBytesInCell;
const headers = [];
for (let i = 0; i < amountOfColumns; i++) {
const headerOffset = headersOffset + i * amountOfBytesInHeader;
const stringValue = readStringValue(headerOffset);
const type = data.readInt32LE(headerOffset + 8);
headers.push({stringValue, type});
}
const rows = [];
for (let i = 0; i < amountOfRows; i++) {
const cells = [];
for (let j = 0; j < amountOfColumns; j++) {
const cellOffset = cellsOffset + i * amountOfBytesInRow + j * amountOfBytesInCell;
const cellValue = readCell(cellOffset);
const columnType = headers[j].type;
cells.push(parseCellValueToType(cellValue, columnType));
}
rows.push(cells);
}
const util = require('util')
console.log(util.inspect(rows, { maxArrayLength: null }))
function parseCellValueToType(cellValue, type) {
switch (type) {
case 1:
const offset = stringsOffset + cellValue.readInt32LE(0);
let lengthUntilNullTermination = 0;
let nextByte;
do {
nextByte = data.readInt8(offset + lengthUntilNullTermination++);
} while (nextByte !== 0)
return data.slice(offset, offset + lengthUntilNullTermination - 1).toString();
case 2:
return cellValue.readFloatLE(0);
case 3:
return !!cellValue.readInt32LE(0);
}
}
function readCell(offset) {
const stringOffset = data.readInt32LE(offset);
const cellValue = data.slice(offset + 4, offset + 8);
return cellValue;
}
function readStringValue(offset) {
const hash = data.slice(offset, offset + 4);
const stringOffset = data.slice(offset + 4, offset + 8);
return {hash, stringOffset};
}
解决方案
推荐阅读
- c# - 解析 DataType 时出现 JSON 反序列化到 DataTable 问题
- javascript - 我可以使用三元运算符来处理 Promise 中的错误吗?
- assembly - 如何反转movl的效果
- java - Java Sigar 巨大的 RAM 使用量
- android - 领域给予错误的结果依赖于不同的
- amazon-web-services - Kops 在为 NS 记录进行 DNS 查找时创建/更新集群错误
- glsl - GLSL 片段着色器中的曲线旋转
- c# - 通过拖放移动画布上的控件
- java - Java 密钥库未能通过 PKCS12 完整性检查,但 keytool 有效
- jquery-ui - 可调整大小的 Jquery 边界