javascript – 读取大文本文件的n行

前端之家收集整理的这篇文章主要介绍了javascript – 读取大文本文件的n行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我拥有的最小文件> 850k线和每条线的长度未知.目标是在浏览器中从该文件中读取n行.完全阅读它不会发生.

这是HTML< input type =“file”name =“file”id =“file”>和JS我有:

  1. var n = 10;
  2. var reader = new FileReader();
  3. reader.onload = function(progressEvent) {
  4. // Entire file
  5. console.log(this.result);
  6.  
  7. // By lines
  8. var lines = this.result.split('\n');
  9. for (var line = 0; line < n; line++) {
  10. console.log(lines[line]);
  11. }
  12. };

显然,这里的问题是它试图首先实现整个文件,然后用换行符拆分它.因此,无论n,它都会尝试读取整个文件,并在文件很大时最终不读取任何内容.

我该怎么办?

注意:我愿意删除整个函数并从头开始,因为我将能够在我们阅读的每一行中调用console.log().

*“每行都是未知长度” – >意味着该文件是这样的:

  1. (0,(1,2))
  2. (1,(4,5,6))
  3. (2,(7))
  4. (3,(8))

编辑:

要走的路就像filereader api on big files,但我看不出如何修改它来读取文件的n行……

通过使用Uint8Array to string in Javascript,可以从那里做到:

  1. var view = new Uint8Array(fr.result);
  2. var string = new TextDecoder("utf-8").decode(view);
  3. console.log("Chunk " + string);

但这可能无法读取整个最后一行,那么您将如何确定以后的行?例如,这是它打印的内容

  1. ((7202),(u'11330875493',u'2554375661'))
  2. ((1667),(u'9079074735',u'6883914476',

解决方法

逻辑非常类似于我在 filereader api on big files的回答中所写的内容,除了你需要跟踪到目前为止已处理的行数(以及到目前为止读取的最后一行,因为它可能尚未结束) .下一个示例适用于与UTF-8兼容的任何编码;如果您需要其他编码,请查看 TextDecoder构造函数的选项.

如果您确定输入是ASCII(或任何其他单字节编码),那么您也可以跳过使用TextDecoder并使用FileReader‘s readAsText method直接将输入读取为文本.

  1. // This is just an example of the function below.
  2. document.getElementById('start').onclick = function() {
  3. var file = document.getElementById('infile').files[0];
  4. if (!file) {
  5. console.log('No file selected.');
  6. return;
  7. }
  8. var maxlines = parseInt(document.getElementById('maxlines').value,10);
  9. var lineno = 1;
  10. // readSomeLines is defined below.
  11. readSomeLines(file,maxlines,function(line) {
  12. console.log("Line: " + (lineno++) + line);
  13. },function onComplete() {
  14. console.log('Read all lines');
  15. });
  16. };
  17.  
  18. /**
  19. * Read up to and including |maxlines| lines from |file|.
  20. *
  21. * @param {Blob} file - The file to be read.
  22. * @param {integer} maxlines - The maximum number of lines to read.
  23. * @param {function(string)} forEachLine - Called for each line.
  24. * @param {function(error)} onComplete - Called when the end of the file
  25. * is reached or when |maxlines| lines have been read.
  26. */
  27. function readSomeLines(file,forEachLine,onComplete) {
  28. var CHUNK_SIZE = 50000; // 50kb,arbitrarily chosen.
  29. var decoder = new TextDecoder();
  30. var offset = 0;
  31. var linecount = 0;
  32. var linenumber = 0;
  33. var results = '';
  34. var fr = new FileReader();
  35. fr.onload = function() {
  36. // Use stream:true in case we cut the file
  37. // in the middle of a multi-byte character
  38. results += decoder.decode(fr.result,{stream: true});
  39. var lines = results.split('\n');
  40. results = lines.pop(); // In case the line did not end yet.
  41. linecount += lines.length;
  42. if (linecount > maxlines) {
  43. // Read too many lines? Truncate the results.
  44. lines.length -= linecount - maxlines;
  45. linecount = maxlines;
  46. }
  47. for (var i = 0; i < lines.length; ++i) {
  48. forEachLine(lines[i] + '\n');
  49. }
  50. offset += CHUNK_SIZE;
  51. seek();
  52. };
  53. fr.onerror = function() {
  54. onComplete(fr.error);
  55. };
  56. seek();
  57. function seek() {
  58. if (linecount === maxlines) {
  59. // We found enough lines.
  60. onComplete(); // Done.
  61. return;
  62. }
  63. if (offset !== 0 && offset >= file.size) {
  64. // We did not find all lines,but there are no more lines.
  65. forEachLine(results); // This is from lines.pop(),before.
  66. onComplete(); // Done
  67. return;
  68. }
  69. var slice = file.slice(offset,offset + CHUNK_SIZE);
  70. fr.readAsArrayBuffer(slice);
  71. }
  72. }
  1. Read <input type="number" id="maxlines"> lines from
  2. <input type="file" id="infile">.
  3. <input type="button" id="start" value="Print lines to console">

猜你在找的JavaScript相关文章