package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
/**
* 用户注册,程序启动后,要求用户输入注册信息:
* 用户名,密码,昵称,年龄
* 其中除了年龄是int值外,其余都是String.然后将信息写入user.txt文件保存
* 每条记录固定占用100字节,
* 其中用户名,密码,昵称为字符串,各占用32字节.//目的是为了修改数据时,因为与原数据长度不匹配,而可能占用其他的位
* 年龄为int值固定的4字节
* @author Lemon
*/
public class RegDemo1 {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
System.out.println("欢迎注册");
System.out.println("请依次输入用户名:");
String username=sc.nextLine();
System.out.println("请依次输入密码:");
String password=sc.nextLine();
System.out.println("请依次输入昵称:");
String nickname=sc.nextLine();
System.out.println("请依次输入年龄");
int age=Integer.parseInt(sc.nextLine());
System.out.println("username:"+username+"password:"+password+"nickname:"+nickname+"age:"+age);
RandomAccessFile raf=new RandomAccessFile("user.txt", "rw");
raf.seek(raf.length());
//写名字
byte[] data=username.getBytes("utf-8");
data=Arrays.copyOf(data, 32);
raf.write(data);
System.out.println(raf.getFilePointer());
//写密码
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.getFilePointer());
System.out.println("录入完毕");
raf.close();
}
}
注册信息录入txt
猜你喜欢
转载自blog.csdn.net/qq_37669050/article/details/97659483
今日推荐
周排行