Ola! Boa Tarde.
Primeiro a classe que controla o arquivo.
/*
* OptionFile.java
*
* Created on 12 de Julho de 2008, 04:13
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author Alirio Oliveira
*/
public class OptionFile {
private FileBean bean;
private File f;
private ObjectInputStream input = null;
private ObjectOutputStream out = null;
private String path;
/** Creates a new instance of OptionFile */
public OptionFile(String path) {
this.path = path;
}
public void analizarFile(){
f = new File(path);
if(f.exists()){
if(f.isFile()){
openFile(path);
}
}else{
try {
f.createNewFile();
ObjectOutputStream _out = new ObjectOutputStream(new FileOutputStream(path));
//grava configurações padrão
_out.writeObject(new FileBean(1,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0));
_out.flush();
_out.close();
//abre fluxo pro programa
openFile(f.getAbsolutePath());
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void openFile(String path){
try{
input = new ObjectInputStream(new FileInputStream(path));
//uma forma de manter os dados no arquivo...
bean = (FileBean)input.readObject();
out = new ObjectOutputStream(new FileOutputStream(path));
/*agora gravo novamente... quando abre o FIleOutputStream
ele zero o file desta forma resolvo isso*/
out.writeObject(bean);
}
catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
catch(IOException ioex){
ioex.printStackTrace();
}
}
public void saveOpt(FileBean bean){
try{
this.openFile(path);
out.writeObject(bean);
out.flush();
}
catch(EOFException eof){
eof.printStackTrace();
}
catch(IOException ioex){
ioex.printStackTrace();
}
}
public FileBean loadOpt(){
try{
this.openFile(path);
bean = (FileBean)input.readObject();
}
catch(EOFException eofe){
eofe.printStackTrace();
}
catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
catch(IOException ioex){
ioex.printStackTrace();
}
return bean;
}
public void closeFile(){
try{
if(input != null){
input.close();
}
}
catch(IOException ioex){
ioex.printStackTrace();
}
}
}
agora a classe Serializable, como podem ver bem simples…
/*
* FileBean.java
*
* Created on 12 de Julho de 2008, 04:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.Serializable;
/**
*
* @author Alirio Oliveira
*/
public class FileBean implements Serializable{
int a,b,c,d, e,f,g,h, i,j,l,m, n,o,p,q, r,s,t,u, y,x;
/** Creates a new instance of FileBean */
public FileBean(){
}
public FileBean(FileBean bean){
this.setGroupA(bean.getGroupA()[0], bean.getGroupA()[1], bean.getGroupA()[2], bean.getGroupA()[3]);
this.setGroupB(bean.getGroupB()[0], bean.getGroupB()[1], bean.getGroupB()[2], bean.getGroupB()[3]);
this.setGroupC(bean.getGroupC()[0], bean.getGroupC()[1], bean.getGroupC()[2], bean.getGroupC()[3]);
this.setGroupD(bean.getGroupD()[0], bean.getGroupD()[1], bean.getGroupD()[2], bean.getGroupD()[3]);
this.setGroupE(bean.getGroupE()[0], bean.getGroupE()[1], bean.getGroupE()[2], bean.getGroupE()[3]);
this.setGroupF(bean.getGroupF()[0], bean.getGroupF()[1]);
}
public FileBean(int a, int b,int c,int d,int e,int f,int g,int h,int i,int j,int l,int m
,int n,int o,int p,int q,int r,int s,int t,int u,int y,int x){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
this.h = h;
this.i = i;
this.j = j;
this.l = l;
this.m = m;
this.n = n;
this.o = o;
this.p = p;
this.q = q;
this.r = r;
this.s = s;
this.t = t;
this.u = u;
this.y = y;
this.x = x;
}
public int[] getGroupA() {
int[] aa = {this.a,this.b,this.c,this.d};
return aa;
}
public void setGroupA(int a, int b, int c, int d){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public int[] getGroupB(){
int[] bb = {this.e, this.f, this.g, this.h};
return bb;
}
public void setGroupB(int e, int f, int g, int h){
this.e = e;
this.f = f;
this.g = g;
this.h = h;
}
public int[] getGroupC(){
int[] cc = {this.i, this.j, this.l, this.m};
return cc;
}
public void setGroupC(int i, int j, int l, int m){
this.i = i;
this.j = j;
this.l = l;
this.m = m;
}
public int[] getGroupD(){
int[] dd = {this.n, this.o, this.p, this.q};
return dd;
}
public void setGroupD(int n, int o, int p, int q){
this.n = n;
this.o = o;
this.p = p;
this.q = q;
}
public int[] getGroupE(){
int[] ee = {this.r,this.s,this.t,this.u};
return ee;
}
public void setGroupE(int r, int s, int t, int u){
this.r = r;
this.s = s;
this.t = t;
this.u = u;
}
public int[] getGroupF(){
int[] ff = {this.y,this.x};
return ff;
}
public void setGroupF(int y, int x){
this.y = y;
this.x = x;
}
}
Meu problema:
Quando chamo o metodo loadOpt() tenho o seguinte erro: EOFE Exception (End Of File Exception)??? nem imagino o porque.
Será que alguem sabe onde ta meu erro?
Grato!