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 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 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 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 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.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
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 program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT