Code example for RandomAccessFile

Methods: setLengthwriteByteswriteIntwriteShort

0
			// 
 
			// Set file length to 0, to prevent unexpected 
			// behaviour in case the file already existed. 
			// 
			randomAccessWriter.setLength(0);
 
			// "RIFF" announcement. 
			// 
			randomAccessWriter.writeBytes("RIFF");
 
			// File size, 0 = unknown. 
			// 
			randomAccessWriter.writeInt(0);
 
			// "WAVE fmt " = WAV format. 
			// 
			randomAccessWriter.writeBytes("WAVE");
			randomAccessWriter.writeBytes("fmt ");
 
			// Sub-chunk size, 16 = PCM. 
			// 
			randomAccessWriter.writeInt(Integer.reverseBytes(16));
 
			// AudioFormat, 1 = PCM. 
			// 
			randomAccessWriter.writeShort(Short.reverseBytes((short) 1));
 
			// Number of channels, 1 = mono, 2 = stereo. 
			// 
			randomAccessWriter.writeShort(
					Short.reverseBytes(numberOfChannels));
 
			// Sample rate. 
			// 
			randomAccessWriter.writeInt(Integer.reverseBytes(sampleRate));
 
			// Byte rate = SampleRate * NumberOfChannels * BitsPerSample / 8. 
			// 
			randomAccessWriter.writeInt(Integer.reverseBytes(sampleRate
					* sampleSize * numberOfChannels / 8));
 
			// Block align = NumberOfChannels * BitsPerSample / 8. 
			// 
			randomAccessWriter.writeShort(Short .reverseBytes(
					(short) (numberOfChannels * sampleSize / 8)));
 
			// Bits per sample. 
			// 
			randomAccessWriter.writeShort(Short.reverseBytes(sampleSize));
 
			// "data" announcement. 
			// 
			randomAccessWriter.writeBytes("data");
 
			// Data chunk size, 0 = unknown. 
			// 
			randomAccessWriter.writeInt(0);
 
			// Clear the byte array. 
			// 
			// Note: Removed but here for inspiration. 
			//