Question

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

IN JAVA.

Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line.

For example if file input is:

This is a test

File output should be

1. This is a test

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BELOW FROM NOTEPAD FILE.

This file contains
lines of text to
determine if you can
properly read and
write from a file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import java.util.*;
import java.io.*;

//class
class ReadWriteFile
{
   //main method
   public static void main(String[] args) throws IOException
   {
       File file = new File("input.txt");
      
       Scanner in = new Scanner(file);
      
       FileWriter fw = new FileWriter("output.txt");
  
PrintWriter out = new PrintWriter(fw);
          
       int i = 1;
      
       while(in.hasNext())
       {
           //read a line from file
           String line = in.nextLine();
          
           //write to console  
           System.out.println(line);
      
           //write to file
           out.printf("%d. %s\n", i, line);
          
           i++;
       }
       //close FileWriter
       fw.close();
      
   }
}

input.txt

This file contains
lines of text to
determine if you can
properly read and
write from a file.

output.txt

1. This file contains
2. lines of text to
3. determine if you can
4. properly read and
5. write from a file.

Add a comment
Know the answer?
Add Answer to:
IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do in java as simply as possible with code available for copy and comments Write...

    Please do in java as simply as possible with code available for copy and comments Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Throw an exception if the file does not exist. An error message should result. For example if file input is: This is a test File output should be 1. This is a test I...

  • Lab Assignment 4B Modify your program in Lab Assignment 4A to throw an exception if the...

    Lab Assignment 4B Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. Lab 4A: Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Test with...

  • Write a program in Java that reads the file and does two things: Write to an...

    Write a program in Java that reads the file and does two things: Write to an output file called dataSwitch.txt the same data from the input file, but switch the order of the data entries on each line. For example, if the first line of the input file is “90001 57110”, the first line of the output file would be “57110 90001”. Additionally, print to the screen the number of lines in the file with a label

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma...

    This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma separated values) text file. The text file will contain comma separated values in each line. Each line contains data in this format: String(20), String(5),String(10), double. There will be multiple lines with each line containing the same number of values. Ask the user to enter the path for the CSV file for reading the file. After reading the data from the file output the data...

  • Using Java how would I write a program that reads and writes from binary or text...

    Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this? Example Output: --------------------Configuration: <Default>-------------------- Enter the file name: kenb Choose binary or text file(b/t): b Choose read or write(r/w): w Enter a line of information to write to the file: lasdklj Would you like to enter another line? Y/N only n Continue? (y/n)y Enter the file name: kenb Choose binary or text file(b/t): b Choose...

  • Java Exceptions Suppose a library is processing an input file containing the titles of books in...

    Java Exceptions Suppose a library is processing an input file containing the titles of books in order to remove duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called uniqueTitles.out. When complete, the output file should contain all unique titles found in the input file. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number...

  • In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...

    In Java. Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a class that writes 10 random doubles to a text file, one number per line. Calls a class that reads the text file and displays all the doubles and their sum accurate to two decimal places. There has to be three classes, main for create random numbers, one to write , one to read all in the same file...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT