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

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

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

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

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

  • (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

    (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons (;) or pound signs (#) should be ignored. All empty lines must be ignored Lines containing a string between square brackets are sections. Within each section, lines may contain numbers or strings. For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that. Example input: ; this is a...

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

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