Question

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 cant seem to attach the file, because it is a .txt but it reads:

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

let me know if you have any questions!

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

SOURCE CODE IN JAVA:

import java.util.Scanner;

import java.io.FileWriter;

import java.io.File;

class Main

{

public static void main(String[] args)

{

//storing names of input and output files

final String txtIn="test.txt", txtOut="test_numbered.txt";

try

{

//opening file for input

Scanner in=new Scanner(new File(txtIn));

//opening file for output

FileWriter out=new FileWriter(new File(txtOut));

int lineNum=1; //to store current line number

while(in.hasNextLine()) //while input file has next line to read

{

String line=in.nextLine(); //reading line

out.write(lineNum+". "+line+"\n"); //writing line with line number

lineNum+=1; //updating line number

}

//closing input and output files

in.close();

out.close();

}

catch(Exception e) //catching exceptions

{

System.out.println(e); //error message

}

}

}

INPUT FILE:

OUTPUT FILE:

Add a comment
Know the answer?
Add Answer to:
Please do in java as simply as possible with code available for copy and comments Write...
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. Provide...

    Please do in java as simply as possible with code available for copy and comments. Provide screenshots of code and how your incorporating the .txt file with the code. Write a program that reads in temperatures from the text file temps.txt (file given in the assignment). Write the minimum temperature, maximum temperature and the new array (temperatures plus 10) to a new file. The temperatures in file are: 83 87 81 88 90 92 95

  • 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...

  • 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.

  • JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following...

    JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following text: MWTaahyiebt_e,c__hnyaoontuc;'e_rste_r_aynr_oert_e_gasoduoipdnp_got_shoeandtl__yty_oot_uhrree__apTdrH_oItgRhrDia_sml__eowtnotere.kr_ss_. Inside puzzle2.txt, write the following text: WTTohhriikssi__niigss,___ttbhhueet___wryrioogunh'gtr__emm_eessshssoaawggieen__gff_rrtoohmme___sswaarmmoppnllgee_22o..nttexxstt Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • Part B: Java Programming Problems. Choose 2 of the following problems. Each solution is worth 20...

    Part B: Java Programming Problems. Choose 2 of the following problems. Each solution is worth 20 marks. You are expected to use comments in your code. Marks will be deducted for incorrect brackets, syntax etc, so be careful as possible (40 marks). 1. Create a new program that: • Reads a sentence from a file; Writes the sentence to another file in reverse; and • Prints a message to the user in a JOptionPane message box when finished. 2. Write...

  • JAVA / please add comments Write code for a method named loadDatagrams() that returns an array...

    JAVA / please add comments Write code for a method named loadDatagrams() that returns an array of type Datagram. The method will read from the filepath C:/folder/grams.dgf and the file contains an array of type Datagram ( you may assume that the class definition is visible to the method) Remember to include appropriate exception handling

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

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