你能在javascript中逐行阅读吗?
问题描述:
有没有办法在javascript中逐行读取文件,特别是文件这是一本字典。我试图建立几个月前制作的java anagram求解器的复制品,但遇到了无法逐行读取文件的问题。我可以下载文件并将其存储在本地,如果能够读取它会有任何区别。
Is there any way to read a file line by line in javascript, specifically this file which is a dictionary. I was trying to build a replica of a java anagram solver I made a few months ago, but hit this problem of not being able to read a file line by line. I could download the file and store it locally if that would make any difference to being able to read it.
答
使用YQL:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fcollections%2Finterfaces%2Fexamples%2Fdictionary.txt%22&format=json&diagnostics=true&callback=cbfunc
这是什么小提琴看起来像:
Here's what the fiddle looks like:
window.callback = function(a) { window.file = a.query.results.body.p; go(); };
$.getScript('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fcollections%2Finterfaces%2Fexamples%2Fdictionary.txt%22&format=json&diagnostics=true&callback=callback');
window.go = function() {
var terms = file.split(' ');
for (var i = 0; i < 100; i++)
console.log(terms[i])
};
这个小提琴只做了前100个,但你明白了(我希望)。
The fiddle only does the first 100 but you get the idea (I hope).