本文主要是介绍API(四)RandomAccessFile,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/*** java.io.RandomAccessFile* 该类是专门用来读写文件数据的,其基于指针操作* * @author LIDN1601806300**/
public class RandomAccessFileDemo {public static void main(String[] args) throws IOException {/** 创建RandomAccessFile常用构造方法:* RandomAccessFile(String path,String mode)* RandomAccessFile(File file,String mode)* * 第一个参数是指定要进行读写操作的文件,(如果进行读写操作时,没有指定文件则RandomAccessFile自动创建一个文件)* 第二个参数为权限,常用的:* "r"表示只读模式* "rw"表示读写模式*/RandomAccessFile raf = new RandomAccessFile("./raf.dat","rw"); /** void write(int d)* 向文件中写入1字节数据,写的是给定的int* 值对应的二进制的”低八位“*/raf.write(1);//一般写入的是(0-255)System.out.println("写出完毕");raf.close();}
}
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/*** RAF读写基本类型数据以及RAF基于指针的操作* @author LIDN1601806300**/
public class RandomAccessFileDemo2 {public static void main(String[] args) throws IOException {RandomAccessFile raf = new RandomAccessFile("type.dat","rw");/** RAF是基于指针进行读写数据的,即:RAF总是* 在指针当前位置读或写字节的,并且读写完毕* 后指针会自动移动到下一字节的位置* * 创建RAF后指针默认在文件的第一个字节位置*///获取指针位置long pos = raf.getFilePointer();System.out.println("pos:"+pos);//将一个int最大值写入文件/* vvvvvvvv* 01111111 11111111 11111111 11111111* * max>>>24* 00000000 00000000 00000000 01111111 11111111 11111111 11111111* max>>>16* 00000000 00000000 01111111 11111111 11111111 11111111* 位移运算:* >>>:将二进制整体向右移动,左面补零*/int max = Integer.MAX_VALUE;raf.write(max>>>24);pos = raf.getFilePointer();System.out.println("pos:"+pos);raf.write(max>>16);raf.write(max>>8);raf.write(max);/** RAF提供了方便写出基本类型数据的方法* writeInt(int d)* 一次性将给定的int值对应的4字节全部写出*/raf.writeInt(max);//连续写出8字节raf.writeLong(123L);raf.writeDouble(123.123);pos = raf.getFilePointer();System.out.println("pos:"+pos);System.out.println("写入完毕");/** void seek(long pos)* 将指针移动到指定位置*/raf.seek(0);System.out.println("pos:"+raf.getFilePointer());/** 若读取四字节int值的过程中发现读取到了末尾* 则抛出EOFException end of file*/int d = raf.readInt();System.out.println(d);System.out.println("pos:"+raf.getFilePointer());//读取long值raf.seek(8);long l = raf.readLong();System.out.println(l);System.out.println("pos:"+raf.getFilePointer());//读取double值double b = raf.readDouble();System.out.println(b);System.out.println("pos:"+raf.getFilePointer());raf.seek(0);long L = raf.readLong();System.out.println(L);raf.close();}
}
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/** * 从文件中读取字节*/
public class RAF_read {public static void main(String[] args) throws IOException {/** "./"不写默认是当前目录*/RandomAccessFile raf= new RandomAccessFile("raf.dat","r");/** int read()* 从文件中读取1个字节,并以int形式返回* 若返回值为-1则表示读取到了文件末尾* * 00000000 00000000 00000000 00000001*/int d = raf.read();System.out.println(d);d = raf.read();System.out.println(d);//-1raf.close();}
}
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.RandomAccess;
/*** 向文件中写入字符串* @author LIDN1601806300**/
public class WriteStringDemo {public static void main(String[] args) throws IOException {RandomAccessFile raf = new RandomAccessFile("pg.txt","rw");String str = "吃得苦中苦,方为人上人";/** String提供了将字符串转换为字节的方法:* byte[] getBytes()* 按照系统默认字符集转换(不推荐,存在平台差异)* * byte[] getBytes(String csn)* 按照给定的字符集转换,字符集的名字不区分大小写* 常见字符集:* GBK:国标编码 中文二字节* UTF-8:unicode的子集,也称为万国码 中文三字节* IOS8859-1:欧洲字符集,不支持中文*/byte[] data = str.getBytes("gbk");raf.write(data);System.out.println("写出完毕");raf.close();}
}
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/*** 读取字符串* @author LIDN1601806300**/
public class ReadStringDemo {public static void main(String[] args) throws IOException {RandomAccessFile raf = new RandomAccessFile("pg.txt","r");byte[] data = new byte[100];//一次读取100字节int len = raf.read(data);/** String提供了构造方法:* String(byte[] data)* 按照系统默认的字符集将字节数组内容转为字符串* * String(byte[] data,String csn)* 按照给定字符集将字符数组内容转换为字符串* * String (byte[] data,int offset,int len,String csn)* 将给定字符数组中从下标offset处的连续len个* * 字节按照给定的字符集转换为对应字符串*///String str = new String(data);//String str = new String(data,"gbk");String str = new String(data,0,len,"gbk");System.out.println(str);raf.close();}
}
package raf;
/** 文件的复制*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class CopyDemo {public static void main(String[] args) throws IOException {RandomAccessFile src = new RandomAccessFile("raf.dat","r");RandomAccessFile dir = new RandomAccessFile("raf_1.dat","rw");int d;//保存每次读取的字节long start = System.currentTimeMillis();while((d=src.read())!=-1) {dir.write(d);}long end = System.currentTimeMillis();src.close();dir.close();System.out.println("复制完毕,耗时为:"+(end-start)+"ms");}
}
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/*** 由于硬盘的物理特性,导致随机读写效率很低,块读写* 的效率还是不错的* 随机读写:单字节读写数据* 块读写:一次一组字节读写数据* * 所以若想提高读写效率我们需要提高每次读写的数据量* 减少实际读写次数的达到* @author LIDN1601806300**/
public class CopyDemo2 {public static void main(String[] args) throws IOException {RandomAccessFile src = new RandomAccessFile("raf.dat","r");RandomAccessFile dir = new RandomAccessFile("raf_2.dat","rw");/** *RAF提供了一次读写一组字节的方法*int read(byte[] data)*一次性读取给定的字节数组总长度的字节量,*并将这些字节存入到该数组中,返回值为本次*实际读取到的字节量,若返回值为-1,则表示*本次读取的是文件末尾**void write(byte[] data)*一次性将给定的字节数组中所有字节写入文件**void write(byte[] data,int offset,int len)*将给定数组从下标offset处的连续len个字节*一次性写出**1byte 8位2进制*1kb 1024byte*1mb 1024kb*1gb 1024m**///10kbbyte[] buf = new byte[1024*10];int len = -1;//实际读取到的字节数long start = System.currentTimeMillis();while((len=src.read(buf))!=-1) {dir.write(buf,0,len);}long end = System.currentTimeMillis();System.out.println("复制完毕,耗时"+(end-start)+"ms");src.close();dir.close();}
}
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;/*** 完成用户注册功能* * 用user.dat文件保存用户信息* 每个用户的信息包括:用户名,密码,昵称,年龄* 其中年龄是int值,其余都是字符串* @author LIDN1601806300**/
public class Test1 {public static void main(String[] args) throws IOException {RandomAccessFile raf = new RandomAccessFile("user.dat","rw");//先将指针移动到文件末尾,以便追加新纪录raf.seek(raf.length());String name = "苍老师";String password = "123456";String nickname = "松";int age = 25;/** 每条记录占用100字节,其中用户名,密码,昵称* 各占32个字节,int型的age占4字节* 数据“留白”的目的是便于后期修改信息*///写用户名byte[] data = name.getBytes("UTF-8");data = Arrays.copyOf(data, 32);raf.write(data);//一次性写32个字节data = password.getBytes("UTF-8");data = Arrays.copyOf(data, 32);raf.write(data);data = nickname.getBytes("UTF-8");data = Arrays.copyOf(data, 32);raf.write(data);raf.writeInt(age);System.out.println("注册完毕");raf.close();}
}
package raf;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;/*** 将用户信息读取出来并输出到控制台* @author LIDN1601806300**/
public class Test2 {public static void main(String[] args) throws IOException {RandomAccessFile raf = new RandomAccessFile("user.dat","r");/** 将每个用户信息输出格式:user,pwd,nick,age*/for(int i=0;i<raf.length()/100;i++) {//读用户名byte[] data = new byte[32];raf.read(data);String name = new String(data,"UTF-8").trim();//读密码raf.read(data);String pwd = new String(data,"UTF-8").trim();//读昵称raf.read(data);String nick = new String(data,"UTF-8").trim();int age = raf.readInt();System.out.println(name+","+pwd+","+nick+","+age);}}
}
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;/*** 查询用户功能* * 程序启动后,要求输入要查询的用户名* 然后将该用户信息输出,格式:user,pwd,nick、age* * 若输入的用户在user.dat文件中不存在,则提示* 查无此人* @author LIDN1601806300**/
public class Test3 {public static void main(String[] args) throws IOException {Scanner scan = new Scanner(System.in);String username = null;while(true) {System.out.println("请输入用户名:");username = scan.nextLine().trim();if(username.length()>=0) {break;}System.out.println("用户名不能为空");}RandomAccessFile raf = new RandomAccessFile("user.dat","r");boolean have = false;//查到用户for(int i=0;i<raf.length()/100;i++) {//移动指针到该记录的用户名位置raf.seek(i*100);byte[] data = new byte[32];raf.read(data);String name = new String(data,"UTF-8").trim();if(name.equals(username)) {//找到了//读密码raf.read(data);String pwd = new String(data,"UTF-8").trim();//读昵称raf.read(data);String nick = new String(data,"UTF-8").trim();//度年龄int age = raf.readInt();have = true;System.out.println(name+","+pwd+","+nick+","+age);break;}}if(!have) {System.out.println("查无此人");}raf.close();}
}
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
/*** 修改密码功能* * 程序启动后,要求用户输入用户名及密码,然后将* user.dat文件中该用户名的密码更改* 若没有该用户,则提示:用户名无效* @author LIDN1601806300**/
public class Test4 {public static void main(String[] args) throws IOException {Scanner scan = new Scanner(System.in);String username = null;String pwd = null;while(true) {System.out.println("请输入用户名:");username = scan.nextLine().trim();System.out.println("请输入密码:");pwd = scan.nextLine().trim();if(username.length()>=0) {break;}System.out.println("用户名不能为空");}RandomAccessFile raf = new RandomAccessFile("user.dat","rw");boolean have = false;//查到用户for(int i=0;i<raf.length()/100;i++) {//移动指针到该记录的用户名位置raf.seek(i*100);byte[] data = new byte[32];raf.read(data);String name = new String(data,"UTF-8").trim();if(name.equals(username)) {//找到了//改密码data = pwd.getBytes("UTF-8");data = Arrays.copyOf(data, 32);raf.write(data);have = true;System.out.println("修改成功");break;}}if(!have) {System.out.println("查无此人");}raf.close();}
}
这篇关于API(四)RandomAccessFile的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!