Question

In: Computer Science

Write a program that reads a file (provided as attachment to this assignment) and write the...

Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).

Solutions

Expert Solution

package file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileTextTransfer {
   public static void main(String[] args) throws IOException {
       File file = new File("."); // . indicates current directory or project.
       String source = file.getCanonicalPath() + File.separator + "Source.txt";
       String dest = file.getCanonicalPath() + File.separator + "Destination.txt";
       // creating a file object
       File srcFile = new File(source);
       // providing actual path of file.
       FileInputStream fistream = new FileInputStream(srcFile);
       //BufferedReader class is used to read the text from a character-based input stream.
       BufferedReader bReader = new BufferedReader(new InputStreamReader(fistream));
       // FileWriter class is used to write character-oriented data to a file along with a boolean
       //indicating whether or not to append the data written. If boolean is true, then data will be
       // written to the end of the file rather than the beginning.
       FileWriter fwriter = new FileWriter(dest, true);
       //BufferedWriter class is used to provide buffering for File Writer instances.
       BufferedWriter outWriter = new BufferedWriter(fwriter);
       String appendLine = null;
       int readCount=1,writeCount=1;
       // while will execute until readLine() is not null.
       while ((appendLine = bReader.readLine()) != null) {
           readCount++;   // reading count increment if reads 1 line.
           //Process each line and add to output to Destination.txt file with line numbers.
           appendLine=writeCount+". "+appendLine;
           // writing line into BufferedWriter.
           outWriter.write(appendLine);
           // after reading line providing new line.
           outWriter.newLine();
           // incrementing writeCount after writing 1 line .
           writeCount++;
       }
       // if reading successfull and writing successfull then only print file transfer successfully.
       if(readCount==writeCount) System.out.println("File text transfer successfully....!!!");
       else System.out.println("File not send successfully.....!!");
       // closing the buffer reader
       bReader.close();
       // closing the buffer writer
       outWriter.close();
   }
}


Related Solutions

In this assignment you will write a PHP program that reads from a data file, creates...
In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table. Create a PHP file called hw4.php that will do the following: - Display your name. - Read text data from a file. The data file is hw3.txt. The file hw3.txt will hold the following text: PQRParrot, Quagga, Raccoon DEFDo statements, Else statements, For statements GHIGeese, Hippos, If statements...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT