The initial charge on the capacitor C1 will be Q=C1V. If after closing the switch the charge flown through the circuit is q, the charge on C2 will be q and C1 will be Q−q. The potential across the capacitors will be same. Hence,
C1Q−q=C2q⇒q=C1+C2C2Q⇒q=C1+C2C1C2V
