
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- How to convert the content of the file into uppercase or lowercase?
- Making a Java String All Uppercase or All Lowercase.
- Java program to convert a string to lowercase and uppercase.
- How to convert all uppercase letters in string to lowercase in Python?
- How to convert all the records in a MySQL table from uppercase to lowercase?
- Convert string to lowercase or uppercase in Arduino
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k in C++
- How to convert lowercase letters in string to uppercase in Python?
- Reading Text File into Java HashMap
- Convert text to uppercase with CSS
- Convert text to lowercase with CSS
- How to convert File into a Stream in Java?
- Write a C program to convert uppercase to lowercase letters without using string convert function
- How to convert a string into uppercase in AngularJS?
- Golang Program to convert Uppercase to Lowercase characters, using binary operator.
