随机文件读写出: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')