@Override
	public boolean export(File file, DomainObject domainObj, AddressSetView addrSet,
			TaskMonitor monitor) throws IOException, ExporterException {

		if (!(domainObj instanceof Program)) {
			log.appendMsg("Unsupported type: " + domainObj.getClass().getName());
			return false;
		}
		Program program = (Program) domainObj;

		Memory memory = program.getMemory();

		if (addrSet == null) {
			addrSet = memory;
		}

		AddressSet set = new AddressSet(addrSet);

		//skip blocks that are not initialized...
		MemoryBlock[] blocks = memory.getBlocks();
		for (int i = 0; i < blocks.length; ++i) {
			if (!blocks[i].isInitialized()) {
				set.delete(new AddressRangeImpl(blocks[i].getStart(), blocks[i].getEnd()));
			}
		}

		try (FileOutputStream fos = new FileOutputStream(file)) {
			AddressRangeIterator iter = set.getAddressRanges();
			while (iter.hasNext()) {
				AddressRange range = iter.next();
				byte[] mem = new byte[(int) range.getLength()];
				int numBytes = memory.getBytes(range.getMinAddress(), mem);
				fos.write(mem, 0, numBytes);
			}
		}
		catch (MemoryAccessException e) {
			throw new ExporterException(e);
		}

		return true;
	}
An {@link Exporter} that can export memory blocks as raw bytes
Ghidra/Features/Base/src/main/java/ghidra/app/util/exporter/BinaryExporter.java