Calcul du nombre de lignes dans un fichier texte en JavaScript
Bonjour,
Après avoir résolu le problème de lecture des fichiers en javascript, je voudrais récupérer chaque ligne à part avec une fonction ReadLine, comme celle de Java, j'ai commencé en première étape par compter le nombre de ligne, normalement ça marche, mais, une alerte indiquant que mon script ne répond pas est affiché, et dans la console d'erreur, j'ai une erreur au niveau du script StrinTokenizer.js, comme suit:" this.str has no propreties", donc si vous voulez bien m'aider à résoudre ce problème, Merci bien :cry:
çi dessous vous trouverez mon script, StringTokenizer.js et ma page html qui fait appel à ce script afin de calculer le nombre de ligne dans mon fichier texte:
StringTokenizer.js
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| function StringTokenizer (str, delim) {
this.str = str;
this.delim = delim;
this.getTokens = getTokens;
this.hasMoreTokens = hasMoreTokens;
this.nextToken = nextToken;
this.countTokens = countTokens;
this.tokens = this.getTokens();
this.actuel = 0;
}
function getTokens() {
var tokens = new Array();
var nt;
var st= new String();
st=this.str;
if (st.indexOf(this.delim) < 0) {
// if (this.str.indexOf(this.delim) < 0) {
tokens[0] = this.str;
return tokens;
}
start = 0;
end = this.str.indexOf(this.delim, start);
var i = 0;
while (this.str.length - start >= 1) {
nt = this.str.slice(start, end);
start = end + 1;
if (this.str.indexOf (this.delim, start + 1) < 0)
end = this.str.length;
else
end = this.str.indexOf (this.delim, start + 1);
nt = trim (nt);
while (nt.slice(0, this.delim.length) == this.delim)
nt = nt.slice(this.delim.length);
nt = trim(nt);
if (nt == "") continue;
tokens[i++] = nt;
}
return tokens;
}
function trim (strToTrim) {
return(strToTrim.replace(/^\s+|\s+$/g, ''));
}
function nextToken() {
if (this.actuel >= this.tokens.length)
return null;
else {
var r = this.tokens[this.actuel];
this.actuel++;
return r;
}
}
function hasMoreTokens() {
return this.actuel < this.tokens.length;
}
function countTokens(){
return this.tokens.length;
} |
CalculNbreLigne.html
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| <html>
<title>FONCTION QUI CALCULE LE NBRE DE LIGNE DANS LE FICHIER TEXTE</title>
<head><script language='JavaScript' src='js/SringTokenizerVendredi.js' ></script>
</head>
<body><script>
var xhr_object = null;
if(window.XMLHttpRequest) // Firefox
xhr_object = new XMLHttpRequest();
else if(window.ActiveXObject) // Internet Explorer
xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
// return;
}
xhr_object.open("GET", "foo.txt", true);
xhr_object.onreadystatechange = function() {
if(xhr_object.readyState == 4)
var g=xhr_object.responseText;
var i=0;
tnz = new StringTokenizer(g,"\n");
while(tnz.hasMoreTokens()){
i++;
}
document.write("Nombre de ligne dans le fichier foo.txt est:"+i);
}
xhr_object.send(null);
</script>
</body>
</html> |