Java 把一个文件中的所有小写文字转换成大写文字?

您所在的位置:网站首页 java大写字母转换为小写字母 Java 把一个文件中的所有小写文字转换成大写文字?

Java 把一个文件中的所有小写文字转换成大写文字?

2024-07-11 15:08| 来源: 网络整理| 查看: 265

Java 把一个文件中的所有小写文字转换成大写文字?

在这篇文章中,我们将在java中把一个文件中的所有小写字母文本改为大写字母文本。假设该文本文件包含以下数据 −

“Merry and Jack are studying.”。

那么在用特定的值更新文本文件后,结果将是 —

“MERRY AND JACK ARE STUDYING.”。

这种修改是在toUpperCase()方法的帮助下完成的。它属于java中的String类。

让我们深入研究这篇文章,了解如何通过使用Java编程语言来完成。

向你展示一些实例 实例一

假设文件的原始内容是—-。

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() – 它用于从控制台读取一行文本,属于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编程语言将文件的所有小写文本转换为大写文本。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3