在 Java 中将文件中的所有小写文本转换为大写?
在本文中,我们将使用 Java 将文件中的所有小写文本更改为大写文本。假设文本文件包含以下数据 -
"Merry and Jack are studying."
然后在使用特定值更新文本文件后,结果将是 -
"MERRY AND JACK ARE STUDYING."
此修改是在 toUpperCase() 方法的帮助下完成的。它属于 java 中的 String 类。
让我们深入研究这篇文章,了解如何使用 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() 方法将 oldContent 中的所有小写文本替换为大写文本。
步骤 4 − 使用 FileWriter () 方法用 newContent 重写输入文本文件。
步骤 5 − 打印结果。
语法
readLine() − 它用于从控制台读取一行文本,属于控制台类 ii java。
toUpperCase() − 将所有小写字母转换为大写字母字母。
write() − 它用于为给定文件写入指定的字符串,属于 java 中的 writer 类。
close() − 如果存在任何流,则用于关闭流并释放流中繁忙的资源。它属于 java 中的 Reader 类。
多种方法
我们提供了不同方法的解决方案。
通过使用静态用户输入
通过使用用户定义方法
让我们逐一查看程序及其输出
注意 − 这些程序可能不会在任何在线 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 编程语言将文件中的所有小写文本转换为大写文本的不同方法。
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

