- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用以下代码从本地系统读取文件:
var fs = require('fs');
var text = fs.readFileSync("./men.text");
var textByLine = text.split("\n")
console.log(textByLine);
最佳答案
注意: fs
是一个nodejs模块,您不能在浏览器中使用它。
导入fs模块,
readFileSync
将为您提供缓冲区
要使用split()
函数,您必须将Buffer
转换为String
var fs = require('fs')
var text = fs.readFileSync("./men.text");
var string = text.toString('utf-8') // converting the Buffer into String
var textByLine = string.split("\n")
console.log(textByLine);
<小时/>
▼ 更新 ▼
<小时/>服务器端
fs
是一个nodejs内置模块,您不能在浏览器(客户端)中使用它。在服务器端使用fs
进行操作,获取所需类型的数据和格式,然后您可以使用html
、ejs
等等进行渲染
..模板引擎
这里我使用express创建了一个Nodejs服务器,从浏览器中点击http://localhost:8000/
你将获得数据数组
您可以格式化数据并使用 res.render
将其呈现为 .ejs
或 html
文件
app.js
var express = require('express');
var app = express();
var fs = require('fs')
app.get('/', function (request, response) {
var text = fs.readFileSync("./men.text");
var string = text.toString('utf-8')
var textByLine = string.split("\n")
console.log(textByLine);
response.send(textByLine);
});
app.listen('8000');
<小时/>
虚拟输出:
关于javascript - 未捕获的类型错误 : fs. readFileSync 不是控制台中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199224/
我尝试使用方法获取文件 readFileSync : import fs from 'fs'; import path from 'path'; const templateFile = fs.rea
我对 Node.js 比较陌生,一直在四处寻找,但找不到解决方案。我确实检查了需要的 javascript 文件,它似乎没有“readFileSync”的方法。也许我没有合适的要求文件?我很难找到这个
我正在开发Electron应用程序。 我有src文件夹中的几个JSON文件,需要在应用初始化(设置,配置等)期间将它们复制到用户文件夹中。 在开发人员模式下以及当我对JSON文件进行import时,它
我有一个上传文件的代码这里是代码: var oriPath = JSON.stringify(req.files.profilePicture); va
我尝试了所有这些步骤来将 bpmn 属性面板集成到本地主机中,例如 Integration of Properties panel in localhost . 此后我得到了未捕获错误:fs.read
我正在尝试在 nodejs 的帮助下获取文件内容fs.readFileSync()像这样将函数转换为字符串: let fileData = fs.readFileSync('./path, 'utf8
在我的 Node 应用程序中,我有一个 init 函数: function init() { if (!fs.existsSync("../schedule.json")) f
我在名为 data 的目录下创建了多个函数并用名为 generatedRandomData 的函数返回的一些随机数据填充它们。为了创建多个文件,我编写了这些函数: const createFile =
基本上,我使用 Node 的 FS 模块来读取和写入 JSON 文件。编写它完全可以正常工作,但由于某种原因,读取它会返回一个空字符串。 这个文件叫做records.js const dir = 's
我写了一个简单的 JS 对象,它具有函数 csvFileToArray。函数应返回已解析的 CSV 数组。 问题是我没有传递给 readFileSync 的匿名函数的输出。 test1 已正确登录到控
我想创建从数据库中获取数据的函数,例如 executeQuery(sql)。但我不想从这个函数返回任何 promise ,也不想传递任何回调函数。我只想返回查询的结果。 例如: var rows =
我有一个脚本,可以将数据从 API 写入某些文件。我有一个对象,其中包含每个文件的文件描述符: var csvFds = { 'file1' : null, 'file2' : nul
我正在使用 Insert-css 将一个 css 文件导入到我的 npm 模块中但我得到一个错误。 var fs = require('fs'); var inserCss = require('in
这个问题已经有答案了: Anonymous function passed to readFileSync is not returning any data (2 个回答) 已关闭 6 年前。 我正
我正在尝试使用 html-pdf Node 模块从 html 生成 pdf。我目前正在使用 cloud9 IDE 运行它。 我的代码是: var fs = require("fs"); var pdf
我正在尝试获取 webpack 和 mapbox-gl在 Meteor 系统中协同工作。我到处都查看了上述错误,但没有一个有效。这是我的 webpack 设置 { "root": "src",
我正在尝试通过我正在制作的 Chrome 应用程序将十六进制文件刷新到 Arduino。我使用了基本的 NodeJS 代码(如下所示),并使用 browserify 对其进行了转换,以便它可以在浏览器
为什么这段代码不起作用?如果我评论 fs.readFileSync('file.html');代码有效并创建文件“file.html”但如果我取消注释, fs.writeFileSync 将不起作用,
考虑以下 Node.js Javascript 程序: var fs = require('fs'); var encoding1='?'; var encoding2='?'; var a = fs
以下是 NodeJS 代码示例(同步版本): var fs = require('fs'); var path = './parseLogFiles/reports'; var counter = 0
我是一名优秀的程序员,十分优秀!