日期:2014-05-20  浏览次数:20774 次

这个代码怎么改就正确了?
随机文件读写出:RandomAccessFile类


Java code

import java.io.*;
public class ReadFileRW
{
    public static void main(String []args)
    {
        StringBuffer sb=new StringBuffer();
        char ch;
        try
        {
            while((ch=(char)System.in.read())!="\n")
            {
                sb.append(ch);
            }
            RandomAccessFile raf=new RandomAccessFile("D:\\HelloWorld\\User.txt","rw");
            raf.seek(raf.length());
            raf.writeBytes(sb.toString());
            raf.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}



------解决方案--------------------
while ((ch = (char) System.in.read()) != '\n')