Javascript读取大文件失败

时间:2017-07-09 09:08:23

标签: javascript file io

JSON文件为6 GB。使用以下代码阅读时,

var fs = require('fs');
var contents = fs.readFileSync('large_file.txt').toString();

出现以下错误:

buffer.js:182
    throw err;
    ^

RangeError: "size" argument must not be larger than 2147483647
    at Function.Buffer.allocUnsafe (buffer.js:209:3)
    at tryCreateBuffer (fs.js:530:21)
    at Object.fs.readFileSync (fs.js:569:14)
    at Object.<anonymous> (/home/readHugeFile.js:4:19)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:5)

public class RegisterApplicationServer { final static String PATH = "/base"; static ServiceDiscovery<InstanceDetails> serviceDiscovery = null; public static void main(String[] args) throws Exception { CuratorFramework client = null; try { client = CuratorFrameworkFactory.newClient("192.168.149.129:2181", new ExponentialBackoffRetry(1000, 3)); client.start(); JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>( InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH) .serializer(serializer).build(); serviceDiscovery.start(); // SOME OTHER CODE THAT TAKES CARES OF USER INPUT... } finally { CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(client); } } private static void addInstance(String[] args, CuratorFramework client, String command, ServiceDiscovery<InstanceDetails> serviceDiscovery) throws Exception { // simulate a new instance coming up // in a real application, this would be a separate process if (args.length < 2) { System.err.println("syntax error (expected add <name> <description>): " + command); return; } StringBuilder description = new StringBuilder(); for (int i = 1; i < args.length; ++i) { if (i > 1) { description.append(' '); } description.append(args[i]); } String serviceName = args[0]; ApplicationServer server = new ApplicationServer(client, PATH, serviceName, description.toString()); server.start(); serviceDiscovery.registerService(server.getThisInstance()); System.out.println(serviceName + " added"); } private static void deleteInstance(String[] args, String command, ServiceDiscovery<InstanceDetails> serviceDiscovery) throws Exception { // in a real application, this would occur due to normal operation, a // crash, maintenance, etc. if (args.length != 2) { System.err.println("syntax error (expected delete <name>): " + command); return; } final String serviceName = args[0]; Collection<ServiceInstance<InstanceDetails>> set = serviceDiscovery.queryForInstances(serviceName); Iterator<ServiceInstance<InstanceDetails>> it = set.iterator(); while (it.hasNext()) { ServiceInstance<InstanceDetails> si = it.next(); if (si.getPayload().getDescription().indexOf(args[1]) != -1) { serviceDiscovery.unregisterService(si); } } System.out.println("Removed an instance of: " + serviceName); } } 的最大大小(Buffer在内部用于保存文件数据的大小)约为2GB(来源:https://nodejs.org/api/buffer.html#buffer_buffer_kmaxlength)。

您可能需要一个流JSON解析器,如JSONStream来处理您的文件:

readFileSync()