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

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.
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
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
2. Write a program which reads in the text file generated in part one and writes...
2. Write a program which reads in the text file generated in part one and writes out the same data as a comma-separated values (CSV) file for further processing. The file should have a header line which identifies which column contains which values and should look something like this: Time, Potentiometer, Temperature, Light, Switch0, Switch1, Switch2, Switch3 That header line should be followed by detail lines containing the measurements and should look something like this (matching the above Arduino output):...
Write an assembly language program that reads move review information from a text file and reports...
Write an assembly language program that reads move review information from a text file and reports the overall scores for each movie as well as identifying the movie with the highest total score. There are four movie reviewers numbered from 1 to 4. They are submitting reviews for five movies, identified by the letters from “A” through “E”. Reviews are reported by using the letter identifying the movie, the review rating, which is a number from 0 to 100, and...
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT