Java.io----RandomAccessFile文件操作

11 篇文章 0 订阅

RandomAccessFile

  • read

     //从raf中读出东西放在b数组中
     raf.read(b);
     //从raf中读出东西存放到b数组中,从第十个位置开始存放,共50个元素
     raf.read(b, 10, 50);
     //读取一行
     raf.readLine()
    
  • write

      raf.writeInt(10);
      raf.seek(file.length());  //从文件末尾开始写入
      raf.writeChars("Hello World!!!"); //写入Hello World!!!
    
用户注册
public static void main(String[] args) throws IOException {

    Scanner sc = new Scanner(System.in);
    System.out.println("请输入用户名:");
    String name = sc.next();
    
    //判断输入的用户名是否与正则表达式匹配
    if(!name.matches(("^[a-zA-Z]+$"))){
        System.out.println("你输入的用户名不符合规则,请重新输入!!!");
        name = sc.next();
        if(!name.matches(("^[a-zA-Z]+$"))){
            System.out.println("不符合规则,注册失败,退出程序!");
            System.exit(0);
        }
    }
    System.out.println("请输入密码:");
    String pwd = sc.next();

    RandomAccessFile raf = new RandomAccessFile("D:/code/aa.txt", "rw");//可读可写

    raf.writeBytes(name);//写入用户名
    raf.writeBytes("#");
    raf.writeBytes(pwd);//写入密码

    raf.close();//关闭
    System.out.println("注册成功!!!");	
	}
}
用户登录
public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入用户名:");
    String name = sc.next();
    System.out.println("请输入密码:");
    String pwd = sc.next();

    RandomAccessFile raf = new RandomAccessFile("D:/code/aa.txt","r");//只读

    String str = raf.readLine();//读取一行

    String [] arr =str.split("#");//以#为界拆分为String类型的数组		

    if( name.equals(arr[0]) && pwd.equals(arr[1])){
        System.out.println("登录成功!");
    }else{
        System.out.println("登录失败!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值