Question

Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following...

Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement.

You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project.

FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each line to the screen. You must implement the file read in a try/catch block rather than throwing the exception on the main method. The name of the text file you will be using is jabberwock.txt.

NEXT: Modify your code so that the program takes the name of two files from the command line, opens the first file if it exists for reading and the second file for writing and copies the text of the first file into the second file. As with the first step, you must implement this in a try/catch block with an appropriate error message in the catch block.

NEXT: Complete the method reverse that takes a String and returns a new String that is the original String reversed. Note that we did this method in the lab a few weeks ago, so if you have that code you can just paste it in here.

NEXT: Modify your code so that instead of printing each line to the file, it prints the reverse of each line to the output file using your reverse method.

FINALLY: Make a single change to your code so that instead of copying the file, it outputs the new file not just reversed, but with all characters transformed to uppercase letters. (HINT: Make use of String's toUpperCase() method for this).

Use the code skeleton below to start your code. Then look at the course notes on File I/O to fill in what you need in the skeleton. Your output to the screen should look like the following to match the test cases:

Enter a filename: jabberwock.txt
Enter an output file: output.txt
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In your question, it is given to modify hence i put them under comments which you can remove and implement

import java.io.*;
class Reverse
{
String a="";
Reverse(String b)
{
a=b;
}
public String reverse()throws java.lang.NullPointerException
{
char[] a1 = a.toCharArray();
String b="";
for (int i = a1.length-1; i>=0; i--)
b=b+a1[i];
return b;
}
}
public class Main
{
public static String reverse(String a)
{
char[] a1 = a.toCharArray();
String b="";
for (int i = a1.length-1; i>=0; i--)
b=b+a1[i];
return b;
}   
public static void main(String []args)throws IOException, java.lang.NullPointerException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a file name");
String input=br.readLine();
System.out.println("Enter output file name");
String output=br.readLine();
BufferedReader objReader = null;
BufferedReader objReader2 = null;
try
{
File file = new File(output);
BufferedWriter bw = null;
   if (!file.exists()) {
   file.createNewFile();
   }
FileWriter fw = new FileWriter(file);
   bw = new BufferedWriter(fw);
  
String strCurrentLine;
objReader = new BufferedReader(new FileReader(input));
objReader2 = new BufferedReader(new FileReader(input));
while ((strCurrentLine = objReader.readLine()) != null)
{
System.out.println(strCurrentLine);
bw.write(strCurrentLine);
}
/*System.out.println("Reverse order of strings");
Main m=new Main();
while ((strCurrentLine = objReader2.readLine()) != null)
{
System.out.println(m.reverse(strCurrentLine));
bw.write(strCurrentLine);
//bw.write(toUpperCase(strCurrentLine));
}*/
  
}
catch (IOException e)
{
e.printStackTrace();
}
/*finally
{
try
{
if (objReader != null)
objReader.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}*/
}
}

Add a comment
Know the answer?
Add Answer to:
Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following...
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
  • FIRST: Write code that prompts the user for the name of a text file, opens that...

    FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each line to the screen. You must implement the file read in a try/catch block rather than throwing the exception on the main method. NEXT: Modify your code so that the program takes the name of two files from the command line, opens the first file if it exists for...

  • Overview These exercises will allow you to have some practice with basic Java file Input/Output. In...

    Overview These exercises will allow you to have some practice with basic Java file Input/Output. In addition, you will also have an opportunity to have more practice with methods and arrays.   Objectives Practice with programming fundamentals Variables - Declaration and Assignment Primitive types Arithmetic Expressions Simple keyboard input and text display output Branching - if-elseif-else syntax Loops - simple while loops, nested while loops Methods - functions and procedures ArrayLists - collections of variables File I/O Works towards the following...

  • *Java* You will write a program to do the following: 1. Read an input file with...

    *Java* You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • python Create a program to open a text file for reading, find the maximum number in...

    python Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

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