Question

In: Computer Science

Using Node.js, show how you would read a file and and write out its contents to...

Using Node.js, show how you would read a file and and write out its contents to a new file in reverse order.

As an example, if the source file had:

Roses are red,

Violets are blue.

If the Trump administration continues to lose cabinet members,

I will not be sad. Boo hoo hoo.

The destination file would have:

I will not be sad. Boo hoo hoo.

If the Trump administration continues to lose cabinet members, Violets are blue.

Roses are red,

Solutions

Expert Solution

First install package:

Install reverse-line-reader and ga package using 
npm i reverse-line-reader fs

Code:

var lineReader = require('reverse-line-reader');
var fs = require("fs");
var content = []

// read all lines from source file and push it to array
lineReader.eachLine('source.txt', function(line) {
  content.push(line);
}).then(function () {
    //write array to destination file
    fs.writeFile('destination.txt',content.join('\n'),function (err) {
       console.log(err ? 'Error :'+err : 'written successfully') }
  );
});

Output:

source.text file:

After executing above code:

destination.txt file:


Related Solutions

Using Node.js with Express, show how you would create the following routes, using these URL's, as...
Using Node.js with Express, show how you would create the following routes, using these URL's, as examples: ~ http:#localhost:4200/books/isbn/978123487999/author This URL will return an author's full name from array of names, mapped by ISBN. ~ http:〃localhost:4200/books This returns all the books (just the book names) from the database.
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
1) How do you read the contents of a text file that contains whitespace characters as...
1) How do you read the contents of a text file that contains whitespace characters as part of its data? 2) What capability does the fstream data type provide that the ifstream and ofstream data types do not? 3) What header file do you need to include in a program that performs file operations? 4) What data type do you use when you want to create a file stream object that can write data to a file? 5) What data...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a JavaScript program to read and analyze the assessment results of a unit under Node.js...
Write a JavaScript program to read and analyze the assessment results of a unit under Node.js host environment. The assessment results are read from the keyboard. The input consists of a list of student results. Each student's result includes the student's name and his or her mark for that unit. These results must be stored in one or several arrays. The input ends when the user types "end". Once all assessment results are read in, your program should produce a...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Write a program that copies the contents of one file to a destination file. This program...
Write a program that copies the contents of one file to a destination file. This program works by first prompting the user for the name of the source and destination files. Be sure to include all necessary error checking, including ensuring that the source file exists. Also, if available, you have to use Linux system calls and not standard C library functions in your program. Write/type your source code in your submission file/document. [Restrictions] For file handling operations, only use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT