showing results for - "file to array javascript"
Clara
12 Aug 2016
1var fs = require('fs');
2var text = fs.readFileSync("./mytext.txt", 'utf-8');
3var textByLine = text.split('\n')
River
11 Jan 2018
1const fs = require('fs')
2const path = require('path')
3
4function tempStorageWindows(nameFile, data) {
5	fs.accessSync(path.resolve(process.env.TMP))
6	let filePath = `${process.env.TMP}/`
7	const fileExist = fs.existsSync(path.resolve(filePath, `${nameFile}`))
8
9	if (!fileExist) {
10		fs.writeFileSync(path.resolve(filePath, `${nameFile}`), `${data.trim()}`, { encoding: 'utf8' })
11		const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
12		return Array.from(
13			new Set([
14				...toArray
15					.map((val) => val.trim().replace(/[\s]/gi, ','))
16					.join('')
17					.trim()
18					.split(',')
19			])
20		)
21	} else {
22		fs.appendFileSync(path.resolve(filePath, `${nameFile}`), `\n${data.trim()}`, { encoding: 'utf-8' })
23		const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
24		return Array.from(
25			new Set([
26				...toArray
27					.map((val) => val.trim().replace(/[\s]/gi, ','))
28					.join('')
29					.trim()
30					.split(',')
31			])
32		)
33	}
34}
35
36function tempStorageLinux(nameFile, data) {
37	let filePath = `/tmp`
38	const fileExist = fs.existsSync(path.resolve(filePath, `${nameFile}`))
39
40	if (!fileExist) {
41		fs.writeFileSync(path.resolve(filePath, `${nameFile}`), `${data.trim()}`, { encoding: 'utf8' })
42		const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
43		return Array.from(
44			new Set([
45				...toArray
46					.map((val) => val.trim().replace(/[\s]/gi, ','))
47					.join('')
48					.trim()
49					.split(',')
50			])
51		)
52	} else {
53		fs.appendFileSync(path.resolve(filePath, `${nameFile}`), `\n${data.trim()}`, { encoding: 'utf-8' })
54		const toArray = fs.readFileSync(path.resolve(filePath, `${nameFile}`), { encoding: 'utf-8' }).split(' ')
55		return Array.from(
56			new Set([
57				...toArray
58					.map((val) => val.trim().replace(/[\s]/gi, ','))
59					.join('')
60					.trim()
61					.split(',')
62			])
63		)
64	}
65}
66
67function checkOs(nameFile, data) {
68	if (process.platform === 'win32') {
69		return tempStorageWindows(nameFile, data)
70	} else {
71		return tempStorageLinux(nameFile, data)
72	}
73}
74
75const res = checkOs('index.txt', 'samsul')
76console.log(res)
queries leading to this page
reading a text file in js arrayload file to array javascriptfile into js array converterhow to read file and store in array jsfile list to array jsfile to array javascript onlinejavascript save file to arrayjavascript read data file into arrayreading file as string in javascriptconvert every line of text file into a javascript array of stringsconvert an array from js into a textfileturn text file into js arrayread a file into array javascriptjavascript text file to arraytext file to array javascriptconvert text file to string javascriptjavascript file to arrayread file to array in javascriptfiles to array javascriptjs file into arrayjs read text file into arrayget array from txt jsjavascript read file and put into arrayjs covert file into arrayfile to array javascriptread file into array jsread content from files in a directory and put it in an array javascriptfilearray to array in jsread a local text file into an array with nodejsconvert txt file into js arrayjs file to arrayjs convert file into arrayjavascript program that reads text file and converts to javascript arraytxt to javascript arraytext file to array in vanila jsstring file to array javascriptjavsascript read file into arrayhow to store a text file in an array javascriptconvert text file to array javascriptreader as text js to stringhow to convert text file to array in jsjs open file into arrayread a local text file into an array with angularconvert txt file to array jsjs read file to arrayjs text file to arrayjava script open file into arrayfile to array javascript