gpt4 book ai didi

java - 以相反顺序写入该行

转载 作者:行者123 更新时间:2023-12-01 12:41:02 24 4
gpt4 key购买 nike

我想以相反的顺序写行,例如输入:第 1 行.第 2 行.第 3 行。输出:第 3 行。第 2 行。第 1 行。

这是下面的代码。

{            
FileReader fr = new FileReader("src/file/in.txt");
FileWriter fw = new FileWriter("src/file/out.txt");
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader buffer = new BufferedReader(fr);
String msg =buffer.readLine();
String[] words = msg.split ("\\.");
System.out.println( "The reversed words are : ");
for(int x = words.length - 1; x >=0; x--) {
bw.write(words[x] + " ");
}
bw.close();
fr.close();
}

最佳答案

关闭流

       try {            
FileReader fr = new FileReader("in.txt");
FileWriter fw = new FileWriter("out.txt");
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader buffer = new BufferedReader(fr);
String msg =buffer.readLine();
String[] words = msg.split (" ");
System.out.println( "The reversed words are : ");
for(int x = words.length - 1; x >=0; x--) {
bw.write(words[x] + " "); // here it is not working
// System.out.print(words[x]+" " );
}
bw.close();
fw.close();
} catch (Exception e) {

}

关于java - 以相反顺序写入该行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25097116/

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