gpt4 book ai didi

java - 读取文本文件,然后将大写转换为小写并写入文本文件

转载 作者:行者123 更新时间:2023-12-01 13:49:37 25 4
gpt4 key购买 nike

我试图读入一个文件,然后写入一个新文件,从而将输入文本文件中的大写字符在新的输出文件中更改为小写。该文件将在没有 if 循环的情况下正常读取和写入,然后当我尝试实现 if 循环来更改大小写时,输出文件将打印空白。任何帮助将不胜感激。

import java.io.*;

public class TextFile {
public static void main (String[] args) throws IOException {
File file1 = new File("intext.txt");
File file2 = new File("outtext.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.isUpperCase(CharCounter)){
Character.toLowerCase(CharCounter);
out.write(ch);
}

in.close();
out.close();
}
}

最佳答案

您的 CharCounter 始终为 0,并且您的比较不正确,请尝试如下操作:

import java.io.*;

public class TextFile {
public static void main (String[] args) throws IOException {
File file1 = new File("intext.txt");
File file2 = new File("outtext.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.isUpperCase(ch)){
Character.toLowerCase(ch);

}
out.write(ch);
}
in.close();
out.close();
}
}

关于java - 读取文本文件,然后将大写转换为小写并写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20071366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com