2016-08-31 5 views
0

私はファイルごとに新しい行を渡したいのですが、どうすればいいですか? ?行ごとにtxtファイルを読むにはどうすればいいですか

var file = "file:///C:/Users/Viktor/Desktop/test.txt"; 

function readTextFile(file,line) { 

    var rawFile = new XMLHttpRequest(); 
    rawFile.open("GET", file, true); 
    rawFile.onreadystatechange = function() { 
    if(rawFile.readyState === 4) { 
     if(rawFile.status === 200 || rawFile.status === 0) { 
     var allText = rawFile.responseText; 

     } 
    } 
    }; 
    rawFile.send(null); 
} 

readTextFile(file); 
+0

新しいラインで分割 – epascarello

答えて

1

文字列を新しい行に分割することができます。

var allText = rawFile.responseText, 
    lines = allText.split(/\n/g); //.split("\n") 
console.log(lines.length); 
+0

ありがとう!また、txtファイルから行を削除することは可能ですか? – Andrew

+0

JavaScript(ブラウザ)には、そのファイルを直接更新する方法がありません。 http://stackoverflow.com/questions/18690450/how-to-generate-and-prompt-to-save-a-file-from-content-in-the-client-browserのようなことをすることができます – epascarello

関連する問題