Alterar Dados Arquivo TXT
Olá, boa tarde.
Tudo bem?
Preciso de uma ajuda aqui.
Seguinte, através desse código eu consigo ler as informações que esta dentro do arquivo txt.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>FileAPI HTML5</title>
<style type="text/css">
#filecontents {
border: double;
overflow-y: scroll;
height: 400px;
}
</style>
</head>
<body>
Por favor selecione arquivo que será lido:<br />
<input type="file" id="txtfiletoread" /><br />
<div>Conteúdo do arquivo:</div>
<div id="filecontents">
</div>
<script>
window.onload = function () {
//Check the support for the File API support
if (window.File && window.FileReader && window.FileList && window.Blob) {
var fileSelected = document.getElementById('txtfiletoread');
fileSelected.addEventListener('change', function (e) {
//Set the extension for the file
var fileExtension = /text.*/;
//Get the file object
var fileTobeRead = fileSelected.files[0];
//Check of the extension match
if (fileTobeRead.type.match(fileExtension)) {
//Initialize the FileReader object to read the 2file
var fileReader = new FileReader();
fileReader.onload = function (e) {
var pontovirgula = ";";
var virgula = ",";
var fileContents = document.getElementById('filecontents');
fileContents.innerText = fileReader.result.replace(virgula, pontovirgula).replace(virgula, pontovirgula);
}
fileReader.readAsText(fileTobeRead);
}
else {
alert("Por favor selecione arquivo texto");
}
}, false);
}
else {
alert("Arquivo(s) não suportado(s)");
}
}
</script>
</body>
</html>
So que o arquivo vem separado por virgulas. Eu quero substituir todas as virgulas por ponto e virgula. Se eu usar dessa forma, ele altera a virgula para ponto e virgula da primeira linha.
<script>
window.onload = function () {
//Check the support for the File API support
if (window.File && window.FileReader && window.FileList && window.Blob) {
var fileSelected = document.getElementById('txtfiletoread');
fileSelected.addEventListener('change', function (e) {
//Set the extension for the file
var fileExtension = /text.*/;
//Get the file object
var fileTobeRead = fileSelected.files[0];
//Check of the extension match
if (fileTobeRead.type.match(fileExtension)) {
//Initialize the FileReader object to read the 2file
var fileReader = new FileReader();
fileReader.onload = function (e) {
var pontovirgula = ";";
var virgula = ",";
var fileContents = document.getElementById('filecontents');
fileContents.innerText = fileReader.result.replace(virgula, pontovirgula).replace(virgula, pontovirgula);
}
fileReader.readAsText(fileTobeRead);
}
else {
alert("Por favor selecione arquivo texto");
}
}, false);
}
else {
alert("Arquivo(s) não suportado(s)");
}
}
</script>
7909377852089;1;8380
7909377852133,1,8380
7909377820729,2,8380
7909377820736,2,8380
7909377820743,2,8380
E também, o ultimo valor eu preciso separar os centavos por , virgula.
Arquivo deve ficar dessa forma quando mostrado na pagina.
7909377852089;1;83,80
7909377852133;1;83,80
7909377820729;2;83,80
7909377820736;2;83,80
7909377820743;2;83,80
Ou seja, contando da direita para esquerda, as duas primeiras posições é o centavos.
Se alguém puder me ajudar, ficarei muito grato.
Att;
Gilberto JrDiscussão (0)
Carregando comentários...