我遇到的问题是我编写的代码将大写字母从一个文件转换为另一个文件中的小写字母。当为一些名为inputtext.txt的文本文件运行代码时,它会创建输出文件,但不会转换大写文本。
import java.io.*;
public class TextFile {
public static void main (String[] args) throws IOException {
// Assume default encoding.
// The name of the file to open for reading.
File filein = new File("inputtext.txt");
// The name of the file to open for writing.
File fileout = new File("outputtext.txt");
char CharCounter = 0;
BufferedReader in = (new BufferedReader(new FileReader(filein)));
PrintWriter out = (new PrintWriter(new FileWriter(fileout)));
int z;
while ((z = in.read()) != -1){
if (Character.isUpperCase(z)){
Character.toLowerCase(z);
}
out.write(z);
}
// Always close files.
in.close();
out.close();
}
}
答案 0 :(得分:0)
您可以逐行阅读,然后在写作时转换为小写。
BufferedReader br = new BufferedReader(new FileReader(new File("a")));
PrintWriter pw = new PrintWriter(new FileWriter(new File("b")));
String line = null;
while((line = br.readLine()) != null){
pw.write(line.toLowerCase());
}
pw.close();
br.close();
答案 1 :(得分:0)
你的代码很好。问题是 Character.toLowerCase(z)不会转换 z 的值,而是返回一个新值,这是变量 z <的小写/强>
int theLowerCaseOfZ = Character.toLowerCase(z);
你可以通过这个轻松解决你的问题
int z;
while ((z = in.read()) != -1){
if (Character.isUpperCase(z)){
// SMOKING GUN!!!
out.write( (int) Character.toLowerCase(z) );
}
}
// Always close files.
in.close();
out.close()
你也可以做
int theLowerCaseOfZ = (int) Character.toLowerCase(z);
out.write(theLowerCaseOfZ);
所有爱 RANA SAHIB :), Rachit的答案也很有用,如果需要,我愿意进一步澄清