📄 fileinout.java
字号:
package com.gaoying.util;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import com.gaoying.comm.Filename;
import com.gaoying.model.Account;
import com.gaoying.model.MyObjecOutputStream;
public class FileInOut implements Filename {
//得到新建用户的账号
public static String getAccNum(){
String num=null;
FileInputStream fin=null;
ObjectInputStream objin=null;
try {
fin=new FileInputStream(SYS_FILE);
objin=new ObjectInputStream(fin);
num=(String)objin.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objin.close();
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long n=Long.parseLong(num)+1;
FileOutputStream fout=null;
ObjectOutputStream objout=null;
try {
fout=new FileOutputStream(SYS_FILE);
objout=new ObjectOutputStream(fout);
objout.writeObject(n+"");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objout.close();
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return num;
}
//建立用户
public static void createAcc(Account acc){
ObjectOutputStream objout=null;
FileOutputStream fout=null;
try {
fout=new FileOutputStream(ACC_FILE,true);
if(Filename.ACC_FILE.exists()&&Filename.ACC_FILE.length()>0){
objout=new MyObjecOutputStream(fout);
}else{
objout=new ObjectOutputStream(fout);
}
objout.writeObject(acc);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objout.close();
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("成功建立对象");
}
//后台读取所有用户
public static Account[] readAllAcc(){
ObjectInputStream objin=null;
FileInputStream fin=null;
ArrayList list=new ArrayList();
try {
fin=new FileInputStream(ACC_FILE);
objin=new ObjectInputStream(fin);
while(true){
Account acc=(Account)objin.readObject();
list.add(acc);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EOFException e) {
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objin.close();
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Account acc[]=new Account[list.size()];
for(int i=0;i<acc.length;i++){
acc[i]=(Account)list.get(i);
}
return acc;
}
//保存所有帐户
public static void saveAllACC(Account[] acc){
ObjectOutputStream objout=null;
FileOutputStream fout=null;
try {
fout=new FileOutputStream(ACC_FILE);
objout=new ObjectOutputStream(fout);
for(int i=0;i<acc.length;i++){
objout.writeObject(acc[i]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objout.close();
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("保存成功");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -