Java 将文本文件转换为所有大写字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22318123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:55:16  来源:igfitidea点击:

Converting text file to all uppercase letters

javaloopswhile-loopjgrasp

提问by nerd4life

I have an assignment for class and I have to make a program that takes an existing file and converts all the letters to uppercase. Below is part of the code (specifically the loop):

我有一个课堂作业,我必须制作一个程序,该程序采用现有文件并将所有字母转换为大写。下面是代码的一部分(特别是循环):

 // Read lines from the file until no more are left.
  while (inputFile.hasNext())
  {
      // Read the next name.
     String friendName = inputFile.nextLine();

      // Need line to make all letters uppercase

  }

  System.out.print("Enter the saved file name: ");
  String filename2 = keyboard.nextLine();

When I run it now: I am able to open the first text file and a second txt file is created, but there is no letters in the new file.

当我现在运行它时:我可以打开第一个文本文件并创建第二个 txt 文件,但新文件中没有字母。

回答by amudhan3093

Try String upper = friendName.toUpperCase(). Now the upperString has all the character in the uppercase.Use it according to your need. Refer String API

试试String upper = friendName.toUpperCase()。现在upper字符串的所有字符都是大写的。根据需要使用它。参考字符串 API

回答by anirudh

Look at the String class documentationand try to find a method to change the string value to all uppercase.

查看String 类文档并尝试找到一种将字符串值更改为全部大写的方法。

Also you need to get the output file name above the whileloop and then write to the output file inside the loop since for every iteration of the loop, the string value of the previous iteration is lost.

您还需要获取while循环上方的输出文件名,然后写入循环内的输出文件,因为对于循环的每次迭代,上一次迭代的字符串值都会丢失。

回答by newuser

Use FileUtilsreadFileToString()and writeFileToString()to do the work simple,

使用FileUtilsreadFileToString()writeFileToString()来完成简单的工作,

String content = FileUtils.readFileToString(new File(FILE_PATH));
FileUtils.writeStringToFile(new File(FILE_PATH), content.toUpperCase());

回答by Hirak

friendName = friendName +System.lineSeparator()+friendName.toUpperCase();

//After the while loop finishes, write the contents of friendName in your output file.

//while 循环结束后,将friendName 的内容写入您的输出文件中。

回答by Rahul Tripathi

You may try like this:

你可以这样尝试:

File file1 = new File("initial.txt");
File file2 = new File("final.txt"); 
char CharCounter = 0;       
BufferedReader in = (new BufferedReader(newFileReader(file1)));
PrintWriter out = (new PrintWriter(new FileWriter(file2)));
int ch;
while ((ch = in.read()) != -1)
{
   if (Character.isLowerCase(ch))
   {
        ch=Character.toUpperCase(ch);// convert assign variable
   }
out.write(ch);
}
in.close();
out.close();

回答by Benjamin

Try This Demo Code:

试试这个演示代码:

       import java.io.*;
       import java.util.*;
       class m{

  public static void main (String[] args) {
     try
    {
     FileReader fr = new FileReader("file.java");
     BufferedReader br = new BufferedReader(fr);
     PrintWriter out = (new PrintWriter(new FileWriter("mahesh.java")));
     String s="";
   while((s = br.readLine()) != null) 
   {
      out.write(s.toUpperCase()+"\n");
    }
  out.close();
  }
    catch(Exception e)
    {
     e.printStackTrace();
    }
   }

  }