Note: it's not a duplicate, because here we want to write not only Objects, but also a whole file, then read it back.
I have created a single File
with 3 Objects using ObjectOutputStream
,
String
String
File
( size is between [1 to 1.5]GB )
Below is my code whichever I have used to write the File
byte[] BUFFER = new byte[1024*32];
FileInputStream fis = new FileInputStream("ThisIsTheFile.xyz");
FileOutputStream fos = new FileOutputStream("abcd.dat", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
String fileId = "BSN-1516-5287B-65893", fTitle = "Emberson Booklet";
for(int i = 0; i < 3; i++){
if(i == 0){
oos.write(fileId.getBytes(), 0, fileId.length());
}else if (i == 1){
oos.write(fTitle.getBytes(), 0, fTitle.length());
}else{
InputStream is = new BufferedInputStream(fis);
int bytesRead = -1;
while((bytesRead = is.read(BUFFER)) != -1){
oos.write(BUFFER, 0, bytesRead);
}
is.close();
}
}
fileId = fTitle = null;
oos.flush();
oos.close();
fos.flush();
fos.close();
fis.close();
Now my problem is:
- I don't want to interrupt the
Java Heap Space
so read & write the largeFile
streams using 32KB bytes buffer technique simultaneously. - Am I writing these three
Object
inside a single File separately & correctly? - Last but not least, How should I retrieve these above said all 3
Object
throughObjectInputStream
from"abcd.dat" File
?
Please help.