I have two connected vectors of arrays of numbers. What is the fastest way to write/read them? Should I use default (de)serialization or some other technique? XML is of course too unefficient.
Write them as a binary file where the first 4 bytes is a count of how many, and every 4 bytes after that is a number.
UPDATE: code sample
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
/**
* Write the numbers in binary.
*/
public class WriteBinary {
public static void main(String[] argv) throws IOException {
Vector<int> numbers = getVectorOfNumbers();
int size = numbers.size();
String FILENAME = "binary.dat";
DataOutputStream os = new DataOutputStream(new FileOutputStream(
FILENAME));
os.writeInt(size);
for(int n : numbers) {
os.writeInt(n);
}
os.close();
System.out.println("Wrote " + size + " numbers to file " + FILENAME);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.