java文件流的用户注册,显示,以及更新

6 篇文章 0 订阅
6 篇文章 0 订阅

注册模块:


/**
 * 完成用户注册功能
 * 程序启动后顺序输入:用户名,密码,昵称,年龄
 * 然后将其写入文件user.dat中保存
 * 其中用户名,密码昵称为String类型,年龄为int
 * 格式定义
 * 每条记录占用100字节
 * 其中用户名,密码,昵称,各占32字节,年龄占4字节
 * @author soft01
 *
 */
public class RegDemo {
    public static void main(String[] args) throws IOException {
        File file=new File("user.dat");
        if(!file.exists()) {
            file.createNewFile();
            System.out.println("文件已经创建");
        }else {
            System.out.println("文件已经存在");
        }
        System.out.println("欢迎注册");
        RandomAccessFile raf=new RandomAccessFile("user.dat","rw");
        raf.seek(raf.length());
        Scanner sc=new Scanner(System.in);
        System.out.println("用户名:");
        String userName=sc.nextLine();
        System.out.println("密码:");
        String userPassword=sc.nextLine();
        System.out.println("昵称:");
        String user=sc.nextLine();
        System.out.println("年龄:");
        int age=sc.nextInt();

 

        byte[] data=("用户名:"+userName).getBytes("utf-8");
        data=Arrays.copyOf(data, 32);
        raf.write(data);
        System.out.println("写出用户名完毕");
        System.out.println("pos:"+raf.getFilePointer());

         data=("密码:"+userPassword).getBytes("utf-8");
        data=Arrays.copyOf(data, 32);
        raf.write(data);
        System.out.println("写出密码完毕");
        System.out.println("pos:"+raf.getFilePointer());

         data=("昵称:"+user).getBytes("utf-8");
        data=Arrays.copyOf(data, 32);
        raf.write(data);
        System.out.println("写出昵称完毕");
        System.out.println("pos:"+raf.getFilePointer());

        raf.writeInt(age);
        System.out.println("写出年龄完毕");
        System.out.println("pos:"+raf.getFilePointer());

    }

}

显示模块:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 显示user.dat文件所有用户信息
 * @author soft01
 *
 */
public class ShowAllUserDemo {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        RandomAccessFile raf=new RandomAccessFile("user.dat","rw");
        for(int i=0;i<raf.length()/100;i++) {
        byte[] data=new byte[32];
        raf.read(data);
        String username=new String(data,"utf-8").trim();
        System.out.println(username);
        System.out.println("pos:"+raf.getFilePointer());
        
        raf.read(data);
        String userPassword=new String(data,"utf-8").trim();
        System.out.println(userPassword);
        System.out.println("pos:"+raf.getFilePointer());
        
        raf.read(data);
        String user=new String(data,"utf-8").trim();
        System.out.println(user);
        System.out.println("pos:"+raf.getFilePointer());
        
        int age=raf.readInt();
    
        System.out.println(age);
        System.out.println("pos:"+raf.getFilePointer());
        
        
        }
        
        
        
    }

}

更新模块:

package raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 修改昵称功能
 * 程序启动后要求用户输入用户名和新昵称
 * 然后将user.dat文件中对应的记录进行修改
 * 如果输入的用户名在user.dat文件中不存在,则
 * 输出:
 * 查无此人
 * @author soft01
 *
 */
public class UpdateDemo {

    public static void main(String[] args) throws IOException {
    Scanner sc=new Scanner(System.in);
    System.out.println("查找的用户名");
    String name=sc.nextLine();
    
        int flag=0;
        RandomAccessFile raf=new RandomAccessFile("user.dat","rw");
        for(int i=0;i<raf.length()/100;i++) {
            raf.seek(i*100);
            byte[] data=new byte[32];
            raf.read(data);
            String username=new String(data,"utf-8").trim();
            if(("用户名:"+name).equals(username)) {
                System.out.println("修改的新昵称");
                String nick=sc.nextLine();
                raf.seek(i*100+64);
                
                 data=("昵称:"+nick).getBytes("utf-8");//先写新昵称
                 data=Arrays.copyOf(data, 32);//新昵称扩容到32位
                raf.write(data);//写入把32位覆盖
                System.out.println("修改完毕");
            }else {
                    flag++;
            }
        }
        
        if(flag==(raf.length()/100)) {
            System.out.println("查无此人");
        }
        raf.close();
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭九钦vlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值