用JAVA实现基本信息存储与读取
功能简介:
存储:用户录入信息(三个字符串),会随机生成一个id号作为此用户的文件夹,返回给用户id号,并把用户输入的信息存入id为文件名的文件中,同时复制根目录的1.jpg到这个文件夹(为了方便,纯粹作为练习)
读取:用户输入它的id,程序会找到相应文件夹,输出用户的信息(三个字符串)
界面如下:
下面为完整代码
package aa;
import java.io.*;
import java.util.*;
public class ab{
public static void main(String args[]) throws IOException{
String filepath="information\\";//文件的相对路径
File dir = new File(filepath);//文件对象
if (!dir.exists()) {//创建目录,不存在时创建
dir.mkdirs();// mkdirs创建多级目录
}
File dirinformaton=new File("information\\table.txt");//找表,方便我们生成id
if (!dirinformaton.exists()) {//表不存在就创造
dirinformaton.createNewFile();// 创建目标文件
}
String[] se=null;//用来读入表中数据
int numstore[]=new int[10000];//用int来存储数据
int numcnt=0;//记录表中数据
try {
FileReader reader=new FileReader(dirinformaton);
BufferedReader br=new BufferedReader(reader);
String line;//用来一行行读入
while((line=br.readLine())!=null){//如果当前行不为空
int tem=Integer.valueOf(line.trim());//则转换成int型
numstore[numcnt++]=tem;//存入int数组
}
br.close();
}catch(IOException e) {
e.printStackTrace();
}
Scanner cin=new Scanner(System.in);
for(;;) {
System.out.print("操作\n");
System.out.print("0:新建并存储文件\n");
System.out.print("1:读出文件\n");
int opr=cin.nextInt();//读入操作
if(opr==0) {//如果是0操作,则为创建,生成一个随机id,且不能在表中存在
Random r=new Random(1);//生成随机数
int ran1;
for(;;) {
boolean findid=false;//boolean类型来判断是否生成了重复的
ran1 =r.nextInt(10000);//生成10000以内的随机数
for(int i=0;i<numcnt;i++)
if(numstore[i]==ran1)findid=true;//如果在表中找到了,说明已经有这个id
if(findid==false)break;//如果没有找到
}
numstore[numcnt]=ran1;//放入新的id
numcnt++;//id数量加一
System.out.print("你的id是"+ran1+'\n');//显示用户获得的id号
try {
FileWriter writer=new FileWriter(dirinformaton);
BufferedWriter out=new BufferedWriter(writer);
for(int i=0;i<numcnt;i++) {//在原来的表后加上新生成的id,一起输出
out.write(numstore[i]+"\n");
}
out.flush();
}catch(IOException e) { //try catch这些语句需要,不然代码中中间的段落会无法使用
e.printStackTrace();
}
writeFile(ran1+"");//执行生成与写入函数
}
else if(opr==1) {
System.out.print("输入id\n");
String text1 = cin.next();//用户输入他的id
readFile(text1);//执行查找函数
}
}
}
public static void writeFile(String text1) {
try {
String filepath = "information\\";//子目录
File dir = new File(filepath+text1+"\\");//生成用户自己的文件夹
if (!dir.exists()) {
dir.mkdirs();// mkdirs创建多级目录
}
File writeName=new File(filepath+text1+"\\"+"info.txt");
System.out.print("输入人物相应信息\n");
Scanner cin=new Scanner(System.in);
if (!writeName.exists()) {
writeName.createNewFile();// 创建目标文件
}
String texta=cin.next();//用户随便输入三个信息
String textb=cin.next();
String textc=cin.next();
FileWriter writer=new FileWriter(writeName);//文件流输入
BufferedWriter out=new BufferedWriter(writer);
out.write(texta+' '+textb+' '+textc+'\n');
out.flush();
//下面部分也随便写了下图片拷贝功能,把根目录的图片复制到用户的个人文件里面
File originalFile = new File("1.jpg");//指定要读取的图片
if(originalFile.exists()) {
File result = new File(filepath+text1+"\\"+"1.jpg");//要写入的图片
if (result.exists()) {//校验该文件是否已存在
result.delete();//删除对应的文件,从磁盘中删除
result = new File(filepath+text1+"\\"+"1.jpg");//只是创建了一个File对象,并没有在磁盘下创建文件
}
if (!result.exists()) {//如果文件不存在
result.createNewFile();//会在磁盘下创建文件,但此时大小为0K
}
FileInputStream inimage = new FileInputStream(originalFile);
FileOutputStream outimage = new FileOutputStream(result);// 指定要写入的图片
int n = 0;// 每次读取的字节长度
byte[] bb = new byte[1024];// 存储每次读取的内容
while ((n = inimage.read(bb)) != -1) {
outimage.write(bb, 0, n);// 将读取的内容,写入到输出流当中
}
//执行完以上后,磁盘下的该文件才完整,大小是实际大小
inimage.close();
outimage.close();// 关闭输入输出流
}
}catch(IOException e) {
e.printStackTrace();
}
System.out.print("新建成功\n");//用户的文件夹和它的信息文件创建成功了
}
public static void readFile(String text1) {
try {
String filePath = "information\\"+text1+"\\info.txt";//读取文件
File pathname=new File(filePath);
if (!pathname.exists()) {
System.out.print("文件不存在\n");//文件不存在直接return到上一层函数,注意exit(0)是直接整个函数退出
return;
}
FileReader reader=new FileReader(pathname);//读入
BufferedReader br=new BufferedReader(reader);
String line;
while((line=br.readLine())!=null){//一行行读入,直到读到文件结尾
System.out.println(line);//在屏幕上输出
}
br.close();//关闭文件
}catch(IOException e) {
e.printStackTrace();
}
}
}