With the help of the url package we can define three new commands: \class, \filedir, and \method.
First of all we load the package url as follows:
\usepackage[obeyspaces,spaces]{url}
The obeyspaces option is needed otherwise all spaces inside \url-like commands are ignored and the option spaces is needed otherwise we won't be able to break lines at whitespaces.
These are the definitions:
\DeclareUrlCommand\class{%
\renewcommand{\UrlBigBreaks}{\do\.}%
\renewcommand{\UrlBreaks}{\do\.}%
}
\DeclareUrlCommand\filedir{%
\renewcommand{\UrlBigBreaks}{\do\\}%
\renewcommand{\UrlBreaks}{\do\\}%
}
\DeclareUrlCommand\method{%
\renewcommand{\UrlBigBreaks}{}%
\renewcommand{\UrlBreaks}{}%
}
In the first case, we are creating a command that allows breaks only when a . is encountered. In the second one a command that allows breaks only when a \ is encountered. In the third case we leave the list blank since spaces are already allowed by the spaces option at loading time.
MWE:
\documentclass{article}
\usepackage[obeyspaces,spaces]{url}
\DeclareUrlCommand\class{%
\renewcommand{\UrlBigBreaks}{\do\.}%
\renewcommand{\UrlBreaks}{\do\.}%
}
\DeclareUrlCommand\filedir{%
\renewcommand{\UrlBigBreaks}{\do\\}%
\renewcommand{\UrlBreaks}{\do\\}%
}
\DeclareUrlCommand\method{%
\renewcommand{\UrlBigBreaks}{}%
\renewcommand{\UrlBreaks}{}%
}
\begin{document}
text text text text text text text text text text text text text text tex \class{java.util.String}
text text text text text text text text text text text text text text tex \filedir{C:\a\b\c}
text text text text text text text text text text text text text text \method{public getText()}
\end{document}
Output:
