Extracting the contents of text in a specified environment into a new file

As well as the options provided by Ulrike, you might also be interested in the extract package, which was written for exactly the problem you're looking to solve.

To export all text within the hypothesis environment to a file called filename, add the following code to the preamble:

\usepackage[active, generate=filename, extract-env={hypothesis}]{extract}

Writing the content of one environment to a file is easy: LaTeX already knows the filecontents-environment. The filecontents packages extends this environment. fancyvrb defines VerbatimOut. The listings package has some internal code (used e.g. by the showexpl) too. If you want to collect the content of more than one environment you will have to change their definition so that the end doesn't close the file.

If you don't care about the line breaks and the comments in your environment you can also try something like this:

\documentclass{article}
\usepackage{environ}

\newwrite\myexport

\makeatletter
\NewEnviron{test}{%
\toks@=\expandafter{\BODY}%
\immediate\write\myexport{\the\toks@}}
\makeatother

\begin{document}
\immediate\openout\myexport=test-export.tex
\begin{test}
\section{blub}
abc % cde
ghi
\end{test}

some text
\begin{test}
 continuation
\end{test}
\immediate\closeout\myexport
\end{document}

As Will Robertson let me know, the extract package makes the task very easy.

For the sake of completeness, the following code in the preamble did what I wanted:

\usepackage[active, generate=filename, extract-env={hypothesis}]{extract}

exporting all text within the hypothesis environment to a file called filename.