java更改本地文本文件,如何阅读本地文本文件?

回答(10)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

你需要检查状态0(当使用 XMLHttpRequest 本地加载文件时,你不要't get a status returned because it'来自 Webserver )

function readTextFile(file)

{

var rawFile = new XMLHttpRequest();

rawFile.open("GET", file, false);

rawFile.onreadystatechange = function ()

{

if(rawFile.readyState === 4)

{

if(rawFile.status === 200 || rawFile.status == 0)

{

var allText = rawFile.responseText;

alert(allText);

}

}

}

rawFile.send(null);

}

并在您的文件名中指定 file:// :

readTextFile("file:///C:/your/path/to/file.txt");

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

访问Javascripture!然后转到 readAsText 部分并尝试示例 . 您将能够知道 FileReader 的 readAsText 功能如何工作 .

var openFile = function(event) {

var input = event.target;

var reader = new FileReader();

reader.onload = function(){

var text = reader.result;

var node = document.getElementById('output');

node.innerText = text;

console.log(reader.result.substring(0, 200));

};

reader.readAsText(input.files[0]);

};

...

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在javascript中引入fetch api之后,读取文件内容变得更加简单 .

reading a text file

fetch('file.txt')

.then(response => response.text())

.then(text => console.log(text))

// outputs the content of the text file

reading a json file

fetch('file.json')

.then(response => response.json())

.then(jsonResponse => console.log(jsonResponse))

// outputs a javascript object from the parsed json

更新30/07/2018(免责声明):

这种技术在Firefox中运行良好,但似乎Chrome的fetch实现在编写此更新之日不支持file:/// URL方案(在Chrome 68中测试) .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

var input = document.getElementById("myFile");

var output = document.getElementById("output");

input.addEventListener("change", function () {

if (this.files && this.files[0]) {

var myFile = this.files[0];

var reader = new FileReader();

reader.addEventListener('load', function (e) {

output.textContent = e.target.result;

});

reader.readAsBinaryString(myFile);

}

});


e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

尝试创建两个函数:

function getData(){ //this will read file and send information to other function

var xmlhttp;

if (window.XMLHttpRequest) {

xmlhttp = new XMLHttpRequest();

}

else {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.onreadystatechange = function () {

if (xmlhttp.readyState == 4) {

var lines = xmlhttp.responseText; //*here we get all lines from text file*

intoArray(lines); *//here we call function with parameter "lines*"

}

}

xmlhttp.open("GET", "motsim1.txt", true);

xmlhttp.send();

}

function intoArray (lines) {

// splitting all text data into array "\n" is splitting data from each new line

//and saving each new line as each element*

var lineArr = lines.split('\n');

//just to check if it works output lineArr[index] as below

document.write(lineArr[2]);

document.write(lineArr[3]);

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

其他例子 - 我的读者使用FileReader类

function PreviewText() {

var oFReader = new FileReader();

oFReader.readAsDataURL(document.getElementById("uploadText").files[0]);

oFReader.onload = function (oFREvent) {

document.getElementById("uploadTextValue").value = oFREvent.target.result;

document.getElementById("obj").data = oFREvent.target.result;

};

};

jQuery(document).ready(function(){

$('#viewSource').click(function ()

{

var text = $('#uploadTextValue').val();

alert(text);

//here ajax

});

});

Source file

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

乔恩佩里曼,

是js可以读取本地文件(请参阅FileReader()),但不能自动读取:用户必须使用html 将文件或文件列表传递给脚本 .

然后使用js可以处理(示例视图)文件或文件列表,它们的一些属性以及文件或文件内容 .

出于安全原因,js不能做的是自动访问(无需用户输入)到他的计算机的文件系统 .

要允许js自动访问本地fs,需要创建一个内部没有js的html文件,而是创建一个hta文档 .

一个hta文件里面可以包含js或vbs .

但是hta可执行文件仅适用于Windows系统 .

这是标准的浏览器行为 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

很可能你已经尝试过,输入“false”如下:

rawFile.open("GET", file, false);

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这可能有所帮助,

var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.onreadystatechange = function () {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

alert(xmlhttp.responseText);

}

}

xmlhttp.open("GET", "sample.txt", true);

xmlhttp.send();

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

$(document).ready(function () {

$.ajax({`enter code here`

url: "TextFile.txt",

dataType: "text",

success: function (data) {

var text = $('#newCheckText').val();

var str = data;

var str_array = str.split('\n');

for (var i = 0; i < str_array.length; i++) {

// Trim the excess whitespace.

str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");

// Add additional code here, such as:

alert(str_array[i]);

$('#checkboxes').append(' ' + str_array[i] + '

');

}

}

});

$("#ckbCheckAll").click(function () {

$(".checkBoxClass").prop('checked', $(this).prop('checked'));

});

});

Select All

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值