How to read and write into file using JavaScript?

后端 未结 17 2501
孤街浪徒
孤街浪徒 2020-11-22 02:00

Can anybody give some sample code to read and write a file using JavaScript?

17条回答
  •  臣服心动
    2020-11-22 02:40

    From a ReactJS test, the following code successfully writes a file:

    import writeJsonFile from 'write-json-file';
    
    const ans = 42;
    writeJsonFile('answer.txt', ans);
    
    const json = {"answer": ans};
    writeJsonFile('answer_json.txt', json);
    

    The file is written to the directory containing the tests, so writing to an actual JSON file '*.json' creates a loop!

提交回复
热议问题