把A中看成STRING类型,然后从最后一读取,再写入B中。不知道这样可以不?
终于把程序写出来了。但是感觉我的算法很糟糕,也没有领会5楼说的采用stringbuilder的方法。厚着脸皮把程序贴出来大家看看吧。欢迎指正。
[CODE]import java.io.*;
public class ReserveCopy {
public ReserveCopy() {
}
public static void main (String[] args)throws IOException {
//创建文件对象
File inputFile=new File("D:\\My Documents\\想飞之心.txt");
File outputFile=new File("D:\\My Documents\\target.txt");
//文件输入和输出流对象
FileReader in=new FileReader(inputFile);
FileWriter out=new FileWriter(outputFile);
String temp="";
String txtStr="";
BufferedReader br=new BufferedReader(in);
//将文件A中的内容读到txtStr中
while(br.readLine()!=null)
{
temp=br.readLine();
txtStr+=temp;
}
char c[]=txtStr.toCharArray();
int i=txtStr.length()-1;
//将A中的内容反序写入B中
while(i>=0)
{
out.write(c[i]);
i--;
}
in.close();
out.close();
}
}[/CODE]
你这段代码有问题!!
刚查了查JDK
StringBuffer类确实有个类叫做reverse()
public StringBuffer reverse()
The character sequence contained in this string buffer is replaced by the reverse of the sequence.
Let n be the length of the old character sequence, the one contained in the string buffer just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.
Returns:a reference to this StringBuffer object.