stringFromFile - what does it exactly?

Hi, what exactly does the “stringToFile” from the Community Commons?  And is it suitable for determining whether a file "A" is identical to file "B" by comparing the two strings of the files? Or is it better to create a hash (MD5?) here?   Thx!
2 answers

It reads the file’s content:

	public static String stringFromFile(IContext context, FileDocument source, Charset charset) throws IOException {
		if (source == null) {
			return null;
		try (InputStream f = Core.getFileDocumentContent(context, source.getMendixObject())) {
			return IOUtils.toString(f, charset);

Here the description from the Mendix Runtime API docs:

  • getFileDocumentContent

    public static getFileDocumentContent​(IContext context, IMendixObject fileDocument)

    Returns contents of a file document as an input stream. The returned stream must be closed when it is not needed anymore. No security checks will be performed.


    context - the context

    fileDocument - the file document from which the contents will be returned


    the input stream of the file content of the given file document


Hi Tjark,


The action will write the string into a file. 

As described in the documentation of the action: “Stores a string into the provided FileDocument, using the specified encoding. Note that destination will be committed”. This can’t be used to compare, because mendix will validate the objects, instead of the content of the file.


StringFromFile, “Reads the contents form the provided file document, using the specified encoding, and returns it as string”.


Using the last option you could compare if the two strings are the same.