java中Io流问题

java中Io流问题请编写一个程序,将一个文本文件中的所有大写字母变成小写字母、小写字母变成大写字母。

import java.io.*;
public class UpTest {
public static void main(String[] args) {
File txt = new File("K:\\IO测试\\大小写转换\\Test.txt");//目标源!
BufferedReader br = null;// 读!
BufferedWriter bw = null;// 写!
StringBuilder stb = new StringBuilder();// 缓存!
try {
br = new BufferedReader(new FileReader(txt));
try {
for (String str = br.readLine(); str != null; str = br.readLine()) {
char[] chs = str.toCharArray();//临时数组!
for (int i = 0; i < chs.length; i++) {//遍历!
if (Character.isUpperCase(chs[i])) {//大小写转换!
chs[i] = Character.toLowerCase(chs[i]);
stb.append(chs[i]);
} else {
chs[i] = Character.toUpperCase(chs[i]);
stb.append(chs[i]);
}
}
stb.append(System.getProperty("line.separator"));//跨平台换行!
}
bw = new BufferedWriter(new FileWriter("K:\\IO测试\\大小写转换\\Test1.txt"));//目标目的地!
bw.write(stb.toString());//写!
bw.flush();//刷新流!
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {//关流!
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-09

发文字,超出限制了,发不了,就发图片吧。

追答

要代码私聊我

第2个回答  2019-12-08
bReader.readLine
按行读取,也就是说直到你从键盘输入换行就是按了回车键,否则是不会输出你打的字符的。你打几个字按下回车再看看。
第3个回答  2020-05-20
小伙子
你换成
StringBuffer
试试FileWriter
fOS
=
new
FileWriter("1.txt",true);这里
的true
是在你的文本里追加的
把他改为false
希望能够帮到你
第4个回答  2018-03-10
public static void main(String[] args) throws IOException {
    // 字节字符转换流
    InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("D:\\store\\test\\111.txt")));
    // 字节字符输出流
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File("D:\\store\\test\\111.txt")));
    int c = -1;
    while ((c = isr.read()) != -1) {
        // A-Z : 65-90
        if (c >= 65 && c <= 90) {
            c += 32; // A和a之间差32
            // a-z : 97-122
        } else if (c >= 97 && c <= 122) {
            c -= 32;
        }
        // 输出
        osw.write(c);
    }
    // 别忘了关流
    isr.close();
    osw.close();
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网