Writing files in Node.js

前端 未结 19 2052
情歌与酒
情歌与酒 2020-11-21 11:57

I\'ve been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

相关标签:
19条回答
  • 2020-11-21 12:13

    I know the question asked about "write" but in a more general sense "append" might be useful in some cases as it is easy to use in a loop to add text to a file (whether the file exists or not). Use a "\n" if you want to add lines eg:

    var fs = require('fs');
    for (var i=0; i<10; i++){
        fs.appendFileSync("junk.csv", "Line:"+i+"\n");
    }
    
    0 讨论(0)
  • 2020-11-21 12:14

    Currently there are three ways to write a file:

    1. fs.write(fd, buffer, offset, length, position, callback)

      You need to wait for the callback to ensure that the buffer is written to disk. It's not buffered.

    2. fs.writeFile(filename, data, [encoding], callback)

      All data must be stored at the same time; you cannot perform sequential writes.

    3. fs.createWriteStream(path, [options])

      Creates a WriteStream, which is convenient because you don't need to wait for a callback. But again, it's not buffered.

    A WriteStream, as the name says, is a stream. A stream by definition is “a buffer” containing data which moves in one direction (source ► destination). But a writable stream is not necessarily “buffered”. A stream is “buffered” when you write n times, and at time n+1, the stream sends the buffer to the kernel (because it's full and needs to be flushed).

    In other words: “A buffer” is the object. Whether or not it “is buffered” is a property of that object.

    If you look at the code, the WriteStream inherits from a writable Stream object. If you pay attention, you’ll see how they flush the content; they don't have any buffering system.

    If you write a string, it’s converted to a buffer, and then sent to the native layer and written to disk. When writing strings, they're not filling up any buffer. So, if you do:

    write("a")
    write("b")
    write("c")
    

    You're doing:

    fs.write(new Buffer("a"))
    fs.write(new Buffer("b"))
    fs.write(new Buffer("c"))
    

    That’s three calls to the I/O layer. Although you're using “buffers”, the data is not buffered. A buffered stream would do: fs.write(new Buffer ("abc")), one call to the I/O layer.

    As of now, in Node.js v0.12 (stable version announced 02/06/2015) now supports two functions: cork() and uncork(). It seems that these functions will finally allow you to buffer/flush the write calls.

    For example, in Java there are some classes that provide buffered streams (BufferedOutputStream, BufferedWriter...). If you write three bytes, these bytes will be stored in the buffer (memory) instead of doing an I/O call just for three bytes. When the buffer is full the content is flushed and saved to disk. This improves performance.

    I'm not discovering anything, just remembering how a disk access should be done.

    0 讨论(0)
  • 2020-11-21 12:14

    Here is the sample of how to read file csv from local and write csv file to local.

    var csvjson = require('csvjson'),
        fs = require('fs'),
        mongodb = require('mongodb'),
        MongoClient = mongodb.MongoClient,
        mongoDSN = 'mongodb://localhost:27017/test',
        collection;
    
    function uploadcsvModule(){
        var data = fs.readFileSync( '/home/limitless/Downloads/orders_sample.csv', { encoding : 'utf8'});
        var importOptions = {
            delimiter : ',', // optional 
            quote     : '"' // optional 
        },ExportOptions = {
            delimiter   : ",",
            wrap        : false
        }
        var myobj = csvjson.toSchemaObject(data, importOptions)
        var exportArr = [], importArr = [];
        myobj.forEach(d=>{
            if(d.orderId==undefined || d.orderId=='') {
                exportArr.push(d)
            } else {
                importArr.push(d)
            }
        })
        var csv = csvjson.toCSV(exportArr, ExportOptions);
        MongoClient.connect(mongoDSN, function(error, db) {
            collection = db.collection("orders")
            collection.insertMany(importArr, function(err,result){
                fs.writeFile('/home/limitless/Downloads/orders_sample1.csv', csv, { encoding : 'utf8'});
                db.close();
            });            
        })
    }
    
    uploadcsvModule()
    
    0 讨论(0)
  • 2020-11-21 12:15

    You can use library easy-file-manager

    install first from npm npm install easy-file-manager

    Sample to upload and remove files

    var filemanager = require('easy-file-manager')
    var path = "/public"
    var filename = "test.jpg"
    var data; // buffered image
    
    filemanager.upload(path,filename,data,function(err){
        if (err) console.log(err);
    });
    
    filemanager.remove(path,"aa,filename,function(isSuccess){
        if (err) console.log(err);
    });
    
    0 讨论(0)
  • 2020-11-21 12:18
     var fs = require('fs');
     fs.writeFile(path + "\\message.txt", "Hello", function(err){
     if (err) throw err;
      console.log("success");
    }); 
    

    For example : read file and write to another file :

      var fs = require('fs');
        var path = process.cwd();
        fs.readFile(path+"\\from.txt",function(err,data)
                    {
                        if(err)
                            console.log(err)
                        else
                            {
                                fs.writeFile(path+"\\to.text",function(erro){
                                    if(erro)
                                        console.log("error : "+erro);
                                    else
                                        console.log("success");
                                });
                            }
                    });
    
    0 讨论(0)
  • 2020-11-21 12:20

    You can of course make it a little more advanced. Non-blocking, writing bits and pieces, not writing the whole file at once:

    var fs = require('fs');
    var stream = fs.createWriteStream("my_file.txt");
    stream.once('open', function(fd) {
      stream.write("My first row\n");
      stream.write("My second row\n");
      stream.end();
    });
    
    0 讨论(0)
提交回复
热议问题