Recently, I've been learning about File I/O in java, and I came to know that Buffered Streams make the whole process more efficient, by storing the data intermittently and only writing to the disk(or reading from it) when the buffer is full.
Now, I have a program where I have an ArrayList
of 256 boolean
, and a floating point value.
I am trying to serialize this data(I do not wish to simply write the data in human-readable form).
So what I do currently is have a FileOutputStream
chained to an ObjectOutputStream
and use its writeObject()
method to first write the boolean
array, and then the float
variable. I do likewise when reading using FileInputStream
and ObjectInputStream
.
Now, since I'm curious how I would go about using the BufferedOutputStream
in here, I went through the documentation but the two overloaded write methods it has don't seem flexible enough to me-
So, would that mean I have to convert my boolean
array to a byte array to be able to use the BufferedOutputStream
object? How then would I convert my float to a byte? I'm also confused on how I would read in the data with BufferedInputStream
.
And would it be worth using Buffers here after all the conversion that's taking place?
Here's my original code without the buffers
public class MySaveListener implements ActionListener { //this is an inner class by the way
public void actionPerformed(ActionEvent ev) {
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(theFrame);
saveFile(fileSave.getSelectedFile());
}
}
private void saveFile(File file){
boolean[] checkboxState = new boolean[256];
for(int i = 0; i < 256; i++){
JCheckBox check = (JCheckBox) checkboxList.get(i);
if (check.isSelected()){
checkboxState[i] = true;
}
}
try{
FileOutputStream fileStream = new FileOutputStream(file);
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(checkboxState);
os.writeObject(1239.87f);
os.close();
} catch(Exception ex){ex.printStackTrace();}
}
public class MyLoadListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
JFileChooser fileOpen = new JFileChooser();
fileOpen.showOpenDialog(theFrame);
loadFile(fileOpen.getSelectedFile());
}
}
private void loadFile(File file){
boolean[] checkboxState = null;
try{
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream is = new ObjectInputStream(fileIn);
checkboxState = (boolean[]) is.readObject();
float loadTempo = (float)is.readObject();
is.close();
} catch(Exception ex){ex.printStackTrace();}
for(int i = 0; i < 256; i++){
JCheckBox check = (JCheckBox) checkboxList.get(i);
if (checkboxState[i]){
check.setSelected(true);
}
else{
check.setSelected(false);
}
}
}
So, what changes would I have to bring in here to make use of BufferedOutputStream
and BufferedInputStream
?
Your understanding of Buffering is not correct. Stream and Writer/Reader buffers are useful if you are doing inefficient writing/reading, such as reading and processing line by line, rather than just trying to read from a stream as fast as you can. On the writing side, it means sending the complete message in 1 call, rather than making several write calls before sending the data. If you are doing efficient reading and writing, you don't need a buffer.
As to the question of how to serialize: The Object streams are easy to use, but end up leaving you with incompatible data eventually. If you want your serialization to be optimal, I'd advise you to convert the boolean[] to a byte[] which you can be written through a regular OutputStream
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With