Java 如何将文件中的所有小写文本转换为大写文本
在本文中,我们将学习如何在Java中将文件中的所有小写文本转换为大写文本。假设文本文件包含以下数据−
“Merry and Jack are studying.”
更新文本文件后,结果将变为−
“MERRY AND JACK ARE STUDYING.”
这是通过使用Java中String类的toUpperCase()方法来实现的。
让我们深入研究本文,了解如何使用Java编程语言来实现。
示例
示例-1
假设文件的原始内容为−
Merry and Jack are studying.
更新文本文件后,结果将变为−
MERRY AND JACK ARE STUDYING.
示例-2
假设文件的原始内容为−
Jack is riding the bicycle.
更新文本文件后,结果将变为−
JACK IS RIDING THE BICYCLE.
步骤
步骤1 −将文件路径作为输入。
步骤2 −使用readLine()方法读取输入文本文件的内容。
步骤3 −使用toUpperCase()方法将旧内容中的所有小写文本替换为大写文本。
步骤4 −使用FileWriter()方法将新内容重新写入输入文本文件。
步骤5 −打印结果。
语法
- readLine() −用于从控制台读取一行文本,它属于Java中的控制台类。
-
toUpperCase() −将所有小写字母转换为大写字母。
-
write() −用于将指定的字符串写入给定的文件,属于Java中的写入器类。
-
close() −用于关闭流并释放流中正在使用的资源(如果存在流的话)。它属于Java中的读取器类。
多种方法
我们提供了多种方法的解决方案。
- 通过使用静态用户输入
-
通过使用用户定义的方法
让我们逐个查看程序及其输出
注意 −这些程序可能在任何在线Java编译器上无法给出预期结果,因为在线编辑器无法访问您本地系统的文件路径。
方法1:通过使用静态用户输入
在这种方法中,文件路径将作为输入。然后根据算法将文件中的所有小写文本更改为大写文本。
示例
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();
}
}
}
}
输出
Original Content of the file: Merry and Jack are studying.
New content of the file: MERRY AND JACK ARE STUDYING.
方法2:通过使用矩阵的动态初始化
在这个方法中,文件路径将作为输入,并调用一个用户定义的方法,根据算法,在文件中将所有的小写文本改为大写文本。
示例
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();
}
}
}
}
输出
Original Content of the file: Jack is riding the bicycle.
New content of the file: JACK IS RIDING THE BICYCLE.
在本文中,我们探讨了使用Java编程语言将文件中所有小写文本转换为大写文本的不同方法。