M
Madhur Ahuja
Hello
I have created a wrapper class for reading and writing objects
in my application. The functions of this class are mainly to
provide ObjectInputStream and ObjectOutputStream objects
to the caller.
The problem I am facing is in close function. How do I ensure
that stream I am closing is really open. Should I just blindly
call ObjectInputStream.close(), even though the stream might
be already closed.
class MyFile
{
private FileInputStream fis;
private FileOutputStream fos;
private ObjectInputStream ois;
private ObjectOutputStream oos;
private String filename;
public BufferedInputStream br;
MyFile(String filename)
{
this.filename=filename;
File ff =new File(filename);
if(ff.exists())
;
else
{
try
{
FileOutputStream fis=new FileOutputStream(filename);
fis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void close()
{
file://if(fis.)
file://fis.close();
file://if(fis.available())
// fos.close();
}
public ObjectInputStream getReader()
{
try
{
fis=new FileInputStream(filename);
file://br=new BufferedInputStream(new FileInputStream());
br=new BufferedInputStream(fis);
ois=new ObjectInputStream(br);
return ois;
}
catch(EOFException e)
{
file://e.printStackTrace();
System.out.println("eof");
return null;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public ObjectOutputStream getWriter()
{
File ff =new File(filename);
try
{
if(ff.exists())
{
fos=new FileOutputStream(filename,true);
}
else
{
fos=new FileOutputStream(filename);
file://fis.close();
}
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
file://br=new BufferedInputStream(new FileInputStream());
try
{
oos=new ObjectOutputStream(fos);
}
catch(Exception e)
{
e.printStackTrace();
}
return oos;
}
}
I have created a wrapper class for reading and writing objects
in my application. The functions of this class are mainly to
provide ObjectInputStream and ObjectOutputStream objects
to the caller.
The problem I am facing is in close function. How do I ensure
that stream I am closing is really open. Should I just blindly
call ObjectInputStream.close(), even though the stream might
be already closed.
class MyFile
{
private FileInputStream fis;
private FileOutputStream fos;
private ObjectInputStream ois;
private ObjectOutputStream oos;
private String filename;
public BufferedInputStream br;
MyFile(String filename)
{
this.filename=filename;
File ff =new File(filename);
if(ff.exists())
;
else
{
try
{
FileOutputStream fis=new FileOutputStream(filename);
fis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void close()
{
file://if(fis.)
file://fis.close();
file://if(fis.available())
// fos.close();
}
public ObjectInputStream getReader()
{
try
{
fis=new FileInputStream(filename);
file://br=new BufferedInputStream(new FileInputStream());
br=new BufferedInputStream(fis);
ois=new ObjectInputStream(br);
return ois;
}
catch(EOFException e)
{
file://e.printStackTrace();
System.out.println("eof");
return null;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public ObjectOutputStream getWriter()
{
File ff =new File(filename);
try
{
if(ff.exists())
{
fos=new FileOutputStream(filename,true);
}
else
{
fos=new FileOutputStream(filename);
file://fis.close();
}
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
file://br=new BufferedInputStream(new FileInputStream());
try
{
oos=new ObjectOutputStream(fos);
}
catch(Exception e)
{
e.printStackTrace();
}
return oos;
}
}