如何阅读JavaScript中的txt文件 [英] How do I read a txt file in javascript

查看:77
本文介绍了如何阅读JavaScript中的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些简单的代码来使用JavaScript读取txt文件.首选是逐条记录,但是如果它也可以放在数组中

I'm looking for some simple code to read a txt file using JavaScript. First choice would be record by record, but if it is in an array that is fine too

推荐答案

JavaScript标准库不包含任何常规I/O操作,因此读取文件必须委托给您的环境提供的其他库.

The JavaScript standard library does not include any general I/O operations, so reading a file must be delegated to some other library provided by your environment.

例如,如果您要针对Web平台,则可以使文本文件从Web服务器可用,并通过XMLHttpRequest进行获取.完全可以使用String切片和正则表达式之类的方法来解析文件.

For example, if you are targeting a web platform you could make the text file available from the web server and fetch it via an XMLHttpRequest. Parsing the file would be up to you entirely using things like String slicing and regular expressions.

如果您要使用node.js或Rhino定位服务器"平台,则可以使用这些环境随附的I/O工具,例如 node.js文件系统接口readFile(filename)(或 Java的FileReader ).其他JavaScript/ECMAScript平台可能会提供自己的实用程序来访问文件系统.

If you are targeting a "server" platform using node.js or Rhino then you can use the I/O facilities provided with those environments, e.g. the node.js FileSystem interface and readFile(filename) (or Java's FileReader), respectively. Other JavaScript/ECMAScript platforms will likely provide their own utilities for accessing the filesystem.

这篇关于如何阅读JavaScript中的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆