当前位置:Gxlcms > 数据库问题 > java下radomAccessFile文件写入读取

java下radomAccessFile文件写入读取

时间:2021-07-01 10:21:17 帮助过:2人阅读

cn.stat.p2.demo; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; public class randomaccessfiledemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub radomwrite(); radomread(); } public static void radomread() throws FileNotFoundException, IOException { RandomAccessFile raf=new RandomAccessFile("demo.txt","rw"); byte[] buf=new byte[4]; raf.read(buf); String name=new String(buf); int age=raf.readInt(); System.out.println("name="+name); System.out.println("age="+age); //获取指针位置 System.out.println("当前位置="+raf.getFilePointer()); //设定指针位置 raf.seek(1*12); System.out.println("age="+raf.readInt()); raf.close(); } public static void radomwrite() throws FileNotFoundException, IOException { RandomAccessFile raf=new RandomAccessFile("demo.txt","rw"); raf.write("张三".getBytes()); raf.writeInt(97); raf.write("赵六".getBytes()); raf.writeInt(98); raf.close(); } }

 

java下radomAccessFile文件写入读取

标签:

人气教程排行