Question

Mainjavo ext.txt Instructions from your teacher 1 My mistress eyes are nothing like the sun; 2 Coral is far more red than heJAVA

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"

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

thanks for the question, here is the code . Please update the below two lines which points to the input file that needs to be read and the location of the output file base don the location in your computer

    private static final String INPUT_FILE = "D:\\artist.txt";
    private static final String OUTPUT_FILE = "D:\\CAPSTEXT.txt";

=======================================================================================

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class Programs {


    private static final String INPUT_FILE = "D:\\artist.txt";
    private static final String OUTPUT_FILE = "D:\\CAPSTEXT.txt";

    public static void main(String[] args) {

        ArrayList<String> fileLines = new ArrayList<>();

        // the below block of code reads from the input file into the ArrayList
        // coverts each line to upper case and add them to the arraylist
       
try {
            Scanner scanner = new Scanner(new File(INPUT_FILE));
            while (scanner.hasNext()) {
                fileLines.add(scanner.nextLine().toUpperCase());
          }

            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("Unable to read from file : " + INPUT_FILE);
            System.exit(1);
        }
        // the above block of code reads from the input file into the ArrayList


        // the below block of code takes each line from the array list and writes them
        // to the output file
       
try {
            PrintWriter writer = new PrintWriter(new FileWriter(OUTPUT_FILE));
            for (String line : fileLines) {
                writer.write(line);
                writer.write("\r\n"); // add new line character to the end
           
}
            writer.flush();
            System.out.println("File created successfully!");

        } catch (IOException e) {
            System.out.println("Unable to write data to file: " + OUTPUT_FILE);
        }

        // the above block of code takes each line from the array list and writes them
        // to the output file
   
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA Write a program that 1. opens a text file 2. reads lines of text from...
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 write a short code that opens a text file called "data.txt" and reads, writes, and...

    Please write a short code that opens a text file called "data.txt" and reads, writes, and keeps writing data to the file in vb.net.

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • Write a program to read a text file, place each line it reads into an array....

    Write a program to read a text file, place each line it reads into an array. Then print the array’s contents one line at a time. Then add to the end of the text file “Success”. Use error exception handling and if the text file does not exist print "Error file not found." Always print "It worked." as part of the try clause. Upload a zip folder file of the .py and text file. roses are roses are red, violets...

  • 7.2 Write a program that prompts for a file name, then opens that file and reads...

    7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution. You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are...

  • Write a program in python that reads each line in a file, reverses its lines, and...

    Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had...

  • 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 question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

  • C++.Write a program that opens two text files and reads their contents into two separate queues....

    C++.Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. Sample Run Suppose we have two files...

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

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

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