javascript convert file to array
var fs = require('fs');
var text = fs.readFileSync("./mytext.txt", 'utf-8');
var textByLine = text.split('\n')
javascript convert file to array
var fs = require('fs');
var text = fs.readFileSync("./mytext.txt", 'utf-8');
var textByLine = text.split('\n')
file to array javascript
const fs = require('fs')
const path = require('path')
function tempStorageWindows(nameFile, data) {
fs.accessSync(path.resolve(process.env.TMP))
let filePath = `${process.env.TMP}/`
const fileExist = fs.existsSync(path.resolve(filePath, `${nameFile}`))
if (!fileExist) {
fs.writeFileSync(path.resolve(filePath, `${nameFile}`), `${data.trim()}`, { encoding: 'utf8' })
const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
return Array.from(
new Set([
...toArray
.map((val) => val.trim().replace(/[\s]/gi, ','))
.join('')
.trim()
.split(',')
])
)
} else {
fs.appendFileSync(path.resolve(filePath, `${nameFile}`), `\n${data.trim()}`, { encoding: 'utf-8' })
const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
return Array.from(
new Set([
...toArray
.map((val) => val.trim().replace(/[\s]/gi, ','))
.join('')
.trim()
.split(',')
])
)
}
}
function tempStorageLinux(nameFile, data) {
let filePath = `/tmp`
const fileExist = fs.existsSync(path.resolve(filePath, `${nameFile}`))
if (!fileExist) {
fs.writeFileSync(path.resolve(filePath, `${nameFile}`), `${data.trim()}`, { encoding: 'utf8' })
const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
return Array.from(
new Set([
...toArray
.map((val) => val.trim().replace(/[\s]/gi, ','))
.join('')
.trim()
.split(',')
])
)
} else {
fs.appendFileSync(path.resolve(filePath, `${nameFile}`), `\n${data.trim()}`, { encoding: 'utf-8' })
const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
return Array.from(
new Set([
...toArray
.map((val) => val.trim().replace(/[\s]/gi, ','))
.join('')
.trim()
.split(',')
])
)
}
}
function checkOs(nameFile, data) {
if (process.platform === 'win32') {
return tempStorageWindows(nameFile, data)
} else {
return tempStorageLinux(nameFile, data)
}
}
const res = checkOs('index.txt', 'samsul')
console.log(res)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us