java 随机流
Example10_8.java
import java.io.*; public class Example10_8 { public static void main(String args[]) { RandomAccessFile inAndOut=null; int data[]={1,2,3,4,5,6,7,8,9,10}; try{ inAndOut=new RandomAccessFile("tom.dat","rw"); for(int i=0;i<data.length;i++) { inAndOut.writeInt(data[i]); } for(long i=data.length-1;i>=0;i--) { //一个int型数据占4个字节,inAndOut从 inAndOut.seek(i*4); //文件的第36个字节读取最后面的一个整数, System.out.printf("\t%d",inAndOut.readInt()); //每隔4个字节往前读取一个整数 } inAndOut.close(); } catch(IOException e){} } }
Example10_9.java
import java.io.*; public class Example10_9 { public static void main(String args[]) { RandomAccessFile in=null; try{ in=new RandomAccessFile("Example10_9.java","rw"); long length=in.length(); //获取文件的长度 long position=0; in.seek(position); //将读取位置定位到文件的起始 while(position<length) { String str=in.readLine(); byte b[]=str.getBytes("iso-8859-1"); str=new String(b); position=in.getFilePointer(); System.out.println(str); } } catch(IOException e){} } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了