📄 objectinandout.java
字号:
package com.zfz.model.DAO;
import java.io.EOFException;
import java.io.File;
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 java.util.Iterator;
import javax.swing.JOptionPane;
import com.zfz.model.Account;
import com.zfz.model.MyObjectOut;
public class ObjectInandOut{
private File afile;
public ObjectInandOut(File afile) {
super();
this.afile = afile;
}
//初始化客户文件
public void createAccFile(){
try {
new FileOutputStream(afile);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void myWriteObj(Object obj){
FileOutputStream fileOut=null;
ObjectOutputStream objOut=null;
try {
fileOut=new FileOutputStream(afile,true);
objOut=null;
if(afile.length()>0){
objOut=new MyObjectOut(fileOut);
}else{
objOut=new ObjectOutputStream(fileOut);
}
objOut.writeObject(obj);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
objOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Account[] myReadObj(){
ArrayList<Account> list = new ArrayList<Account>();
FileInputStream fileIn=null;
ObjectInputStream objin=null;
try {
fileIn=new FileInputStream(afile);
objin=new ObjectInputStream(fileIn);
while(true){
list.add((Account)objin.readObject());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (EOFException e) {
System.out.println("到文件尾 .... ");
}catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
System.out.println("ClassNotFound");
}finally{
try {
objin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int len=list.size();
Account acc[]=new Account[len];
Iterator<Account> it=list.iterator();
int index=0;
while(it.hasNext()){
acc[index++]=(Account)it.next();
}
return acc;
}
public String getAccountNo(){
FileInputStream fileIn=null;
ObjectInputStream objin=null;
String no="";
try {
fileIn=new FileInputStream(afile);
objin=new ObjectInputStream(fileIn);
no=(String)objin.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (EOFException e) {
System.out.println("到文件尾 .... ");
}catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
objin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return no;
}
public void setAccountNo(String no){
FileOutputStream fileOut=null;
ObjectOutputStream objOut=null;
try {
fileOut=new FileOutputStream(afile);
objOut=new ObjectOutputStream(fileOut);
objOut.writeObject(no);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
objOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//---写所有对象并覆盖
public void myWriteAllObj(Account[] acc){
FileOutputStream fileOut=null;
ObjectOutputStream objOut=null;
try {
fileOut=new FileOutputStream(afile);
objOut=new ObjectOutputStream(fileOut);
for(int i=0;i<acc.length;i++){
objOut.writeObject(acc[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
objOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查找账号是否存在
public int getOthNo(Account[] acc,String no,int ind1){
int index=-1;
int i=0;
for(;i<acc.length;i++){
if(acc[i].getNO().equals(no)){
index=i;
if(index==ind1){
JOptionPane.showMessageDialog(null,"不能给自己账户转入");
index=-1;
}
break;
}
}
if(i==acc.length){
JOptionPane.showMessageDialog(null,"转账账号不存在");
}
return index;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -