简体   繁体   English

使用Java读取文本文件并将其存储到数组中

[英]Reading and storing a text file into an array with Javascript

I want to split a text file line by line, and then on encountering a character in the line (':'), to store it in an array. 我想逐行分割文本文件,然后在遇到行(':')中的字符时将其存储在数组中。 I need that array to plot points on a map. 我需要该数组才能在地图上绘制点。 I can do the plotting fine when I do it with a static array. 使用静态数组可以很好地进行绘制。

var map = new google.maps.Map(document.getElementById('map'), googleMap);
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(infowindow, "closeclick", function() {
    map.fitBounds(latlngbound);
    map.panToBounds(latlngbound)
    })
var ipArray = ["70.177.167.189", "123.135.107.115", "123.135.107.115", "123.135.107.115", "123.135.107.115", "122.182.6.19", "24.19.187.145", "24.19.187.145", "24.19.187.145", "93.42.228.21", "82.102.2.210"];

ipArray.forEach((ip) => {
addIPMarker(ip);
})
} catch(e){
    //handle error
}

However, I'm not able to do it when I follow this methodology - 但是,当我遵循这种方法时,我无法做到这一点-

var map = new google.maps.Map(document.getElementById('map'), googleMap);
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(infowindow, "closeclick", function() {
        map.fitBounds(latlngbound);
        map.panToBounds(latlngbound)
})
var data_ip = [];
var textByLine = [];
readTextFile("./2017-12-12.txt", data_ip);
var fs = require("fs");
fs.readFile("./2017-12-12.txt", function(text){
textByLine = (text.split("\n")).split(":")[0];
});

textByLine.forEach((ip) => {
addIPMarker(ip);
})
} catch(e){
    //handle error
}  

Can anyone tell me where I'm going wrong, and how I can correct my code. 谁能告诉我我要去哪里哪里以及如何更正我的代码。 I'm not very familiar with JavaScript. 我对JavaScript不太熟悉。 So, I'm not really sure what exactly I'm doing here. 因此,我不太确定自己到底在做什么。 Any help appreciated! 任何帮助表示赞赏! Thanks! 谢谢!

This presumes that you are using the fs module of node. 假定您正在使用节点的fs模块。

The fs.readFile function is asynchronous and you correctly do the processing into lines in the callback that is called with its result. fs.readFile函数是异步的,您可以正确地将其处理到回调中并以其结果调用的行。

You are however still using the processed textByLine before the callback has executed. 但是,在执行回调之前,您仍在使用已处理的textByLine At that point, the array would still be empty. 那时,该数组将仍然为空。

You can either move the textByLine.forEach part into the callback, right after splitting the file content, or switch to the synchronous version of file reading using readFileSync . 您可以在分割文件内容之后立即将textByLine.forEach部分移动到回调中,也可以使用readFileSync切换到文件读取的同步版本。

The synchronous version would return the content directly and not use a callback which is convenient, it will however block the execution of any remaining part of the script until the file has been read. 同步版本将直接返回内容,而不使用方便的回调,但是它将阻止脚本的任何剩余部分的执行,直到读取文件为止。 This is usually bad for the user experience when processing possibly large files and therefore the asynchronous versions are preferred. 当处理可能较大的文件时,这通常不利于用户体验,因此首选异步版本。

The callback does get two arguments (see the fs.readFile documentation ): err and data . 回调确实有两个参数(请参阅fs.readFile文档 ): errdata If err is set, it means that reading failed for some reason. 如果设置了err ,则表示由于某种原因读取失败。 The actual data is supplied as the second argument. 实际数据作为第二个参数提供。

Last, your callback currently splits the input into lines, and then tries to split the resulting array by the ':' delimiter. 最后,您的回调当前将输入拆分为多行,然后尝试使用':'分隔符将结果数组拆分。 But you indicate that you want each line to be split by the delimiter, so splitting the line should be done inside the textByLine.forEach callback. 但是,您指出您希望每行都由定界符分割,因此分割行应在textByLine.forEach回调内部完成。

Your callback could therefore be adapted like this: 因此,您的回调可以这样修改:

function(err, text) {
    if (err) {
        // handle error somehow
        return;
    }

    text.split('\n').forEach((line) => {
        let ip = line.split(":")[0];
        addIPMarker(ip);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM