RandomAccessFile类的读写 编写一个Java应用程序,利用RandomAccessFile类往某个文本文件中写入20个整数(0~19),然后从该文件的第12个字节开始,将后面所有的数据(对应写入的整数)读出。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void write(){ RandomAccessFile file = null; try{ file = new RandomAccessFile("d:\\a.txt","rw"); for(int i=1;i<=20;i++){ file.writeInt(i); } }catch(Exception e){ e.printStackTrace(); }finally{ try { file.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void read(){ RandomAccessFile file = null; try{ file = new RandomAccessFile("d:\\a.txt","rw"); file.seek(48L); for(int i=0;i<8;i++){ int value = file.readInt(); System.out.println(value); } }catch(Exception e){ e.printStackTrace(); }finally{ try { file.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String []args){ write(); read(); } 哦,write里面的循环写错了,没有看到你写的0-19for(int i=1;i<=20;i++)改成for(int i=0;i<20;i++) 如果是要实现你这个功能建议用BufferedReader,RandomAccessFile易导致编码不一致 有人清楚这里的cat是什么方法? 求java 有关SNMP协议的教程 (从入门到精通的) jbuilder中小程序出问题 一个java基础问题,望高手指教! system.out.write的疑惑-菜鸟问题 怎样配置jdbc的数据库驱动? socket的IO问题 读取.dll文件 如何给一个建好的JTable中增加Row?? 连接数据库报错? java连接数据库,通过servicename而不是sid 如何在java应用程序中加入循环播放的声音
public static void write(){
RandomAccessFile file = null;
try{
file = new RandomAccessFile("d:\\a.txt","rw");
for(int i=1;i<=20;i++){
file.writeInt(i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void read(){
RandomAccessFile file = null;
try{
file = new RandomAccessFile("d:\\a.txt","rw");
file.seek(48L);
for(int i=0;i<8;i++){
int value = file.readInt();
System.out.println(value);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String []args){
write();
read();
}
for(int i=1;i<=20;i++)改成for(int i=0;i<20;i++)