Convert All Lowercase Text of a File into Uppercase in Java?


In this article we are going to change all lower-case text to upper-case text in a file in java. Suppose the text file contains the following data −

“Merry and Jack are studying.”

Then after updating the text file with specific value the result will be −

“MERRY AND JACK ARE STUDYING.”

This modification is done with the help of toUpperCase() Method. It belongs to the String class in java.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Suppose the original content of the file is −

Merry and Jack are studying.

After updating the text file,, the result will be −

MERRY AND JACK ARE STUDYING.

Instance-2

Suppose the original content of the file is −

Jack is riding the bicycle.

After updating the text file,, the result will be −

JACK IS RIDING THE BICYCLE.

Algorithm

Step 1 − Take the file path as an input.

Step 2 − Read the content of the input text file using readLine() method.

Step 3 − Replace all lowercase text with uppercase text in the oldContent using toUpperCase() method.

Step 4 − Rewrite the input text file with newContent using FileWriter () method.

Step 5 − Print the result.

Syntax

  • readLine() − It is used to read a single line of text from the console and it belongs to console class ii java.

  • toUpperCase() − It converts all the smaller Case letters to upper Case letters.

  • write() − It is used to write a specified string for a given file and belongs to the writer class in java.

  • close() − It is used to close the stream and release the resources that were busy in the stream if any stream is present. It belongs to the Reader class in java.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static User Input

  • By Using User Defined Method

Let’s see the program along with its output one by one

Note − These programs may not give the expected result on any online Java compiler. As online editors can not access your local system’s file path.

Approach-1: By Using Static User Input

In this approach, file path will be taken as an input. Then as per the algorithm change all lower-case text to upper-case text in a file.

Example

import java.io.*; public class Main{ public static void main(String[] args){ File fileToBeModified = new File("C:/Users/Kabir/Desktop/Work/file2.txt"); String oldContent = ""; BufferedReader reader = null; FileWriter writer = null; try{ reader = new BufferedReader(new FileReader(fileToBeModified)); String line = reader.readLine(); //Reading the content of input text file while (line != null) { oldContent = oldContent + line + System.lineSeparator(); line = reader.readLine(); } //printing the original content System.out.println("Original Content of the file: " + oldContent); //Replacing lowerCase text to upperCase text String newContent = oldContent.toUpperCase(); //Rewriting the input text file with newContent writer = new FileWriter(fileToBeModified); riter.write(newContent); //Printing the content of modified file //printing the content of the modified file System.out.println("New content of the file: " + newContent); } catch (IOException e){ e.printStackTrace(); } finally{ try{ //Closing the resources reader.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } }

Output

Original Content of the file: Merry and Jack are studying.
New content of the file: MERRY AND JACK ARE STUDYING.

Approach-2: By Using Dynamic Initialization of Matrix

In this approach, file path will be taken as an input. Then call a user defined method and as per the algorithm change all lower-case text to upper-case text in a file.

Example

import java.io.*; public class Main { public static void main(String[] args){ //calling user defined method modifyFile("C:/Users/Kabir/Desktop/Work/file3.txt"); } //user defined method static void modifyFile(String filePath){ File fileToBeModified = new File(filePath); String oldContent = ""; BufferedReader reader = null; FileWriter writer = null; try{ reader = new BufferedReader(new FileReader(fileToBeModified)); //Reading the content of input text file String line = reader.readLine(); while (line != null) { oldContent = oldContent + line + System.lineSeparator(); line = reader.readLine(); } //printing the original content System.out.println("Original Content of the file: " + oldContent); //Replacing the lowerCase text to upperCase text String newContent = oldContent.toUpperCase(); //Rewriting the input text file with newContent writer = new FileWriter(fileToBeModified); //Printing the content of modified file writer.write(newContent); //printing the content of the modified file System.out.println("New content of the file: " + newContent); } catch (IOException e){ e.printStackTrace(); } finally{ try{ //Closing the resources reader.close(); writer.close(); } catch (IOException e){ e.printStackTrace(); } } } }

Output

Original Content of the file: Jack is riding the bicycle.

New content of the file: JACK IS RIDING THE BICYCLE.

In this article, we explored different approaches to convert all lower case text of a file into upper case text by using Java programming language.

Updated on: 01-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements