使用文本文件,存储用户验证信息的问题?
rox 2001-09-14 10:57:01 不好意思,我又上来贴东西了!下面的两个程序:
1、a1915.java
import java.io.*;
class a1915
{
public static void main(String args[])
throws IOException
{
RandomAccessFile raf=new RandomAccessFile("a1915.dat","rw");
DataInputStream dis=new DataInputStream(System.in);
String name=null,s=null;
int sex,height,i,j;
int reclen=16,namelen=11;
long n=0;
while(true)
{
System.out.print("Key in your name:");
System.out.flush();
name=dis.readLine();
if(name.length()==0) break;
System.out.print(" sex:");
System.out.flush();
s=dis.readLine();
sex=Integer.parseInt(s,10);
System.out.print(" height:");
System.out.flush();
s=dis.readLine();
height=Integer.parseInt(s,10);
raf.seek(n*reclen);
n++;
if(name.length()>=namelen)
{
for (j=0;j<namelen;j++)
raf.writeByte((int)name.charAt(j));
}
else
{
for(j=0;j<name.length();j++)
raf.writeByte((int)name.charAt(j));
for(j=name.length();j<namelen;j++)
raf.writeByte(0x20);
}
raf.writeByte(sex);
raf.writeInt(height);
}
raf.close();
}
}
2、a1916.java
import java.io.*;
class a1916
{
public static void main(String args[])
throws IOException
{
RandomAccessFile raf=new RandomAccessFile("a1915.dat","rw");
DataInputStream dis=new DataInputStream(System.in);
String name=null,s=null;
byte sex,ba[]=new byte[18];
int height,i,j;
int reclen=16,namelen=11;
long p,n=raf.length();
while(true)
{
System.out.print("\nkey in record number:");
System.out.flush();
s=dis.readLine();
if(s.length()==0) break;
p=Long.parseLong(s,10);
if(p>n-1)
{
System.out.println("out of range!");
System.exit(1);
}
raf.seek(p*reclen);
for(j=0;j<namelen;j++)
ba[j]=raf.readByte();
sex=raf.readByte();
height=raf.readInt();
System.out.print("name=");
for(j=0;j<namelen;j++)
System.out.print((char)ba[j]);
System.out.print(" sex="+sex);
System.out.print(" height="+height);
System.out.flush();
}
raf.close();
}
}
运行a1915输入数据:
java a1915
Key in your name:张三
sex:1
height:178
使用记事本打开a1915.dat,显示如下:
张三
运行a1916,察看a1916的数据
java a1916
key in record number:0
name=????
sex=1 height=178
1、请问有没有什么办法解决中文问题,
2、小弟要做一个servlet,使用文本文件存储用户的信息。有没有现成的例子或者资料提供,其中对于用户明和密码的检索,是不是一旦用户太大,检索起来就很慢?可不可以支持几个人同时作登陆检查。
谢谢!