[opencms-dev] CmsFile.getContents() method problem: transform to String

opencms at biberbeatz.de opencms at biberbeatz.de
Thu Oct 13 00:26:38 CEST 2005


Hallo List,

I am developing a Java class that runs in the context of OpenCms
therefore I have access to the CmsObject. I want my class to extract
the content of a specific file. The file contains plain text which I
want to extract. I tried it with different file types. Same result.

See my code snip.

The problem is that everythings works until I want to convert the
byte[] (is the return value of the getContents() method) into String.
The result of the transformation is an empty string.

API: http://synyx.de/api/opencms6.0.0/org/opencms/file/CmsFile.html

<snip>

CmsObject cms = OpenCms.initCmsObject("Guest");
cms.loginUser("Admin", "admin");
List filesInFolder = cms.getFilesInFolder("/sites/default/test");
CmsFile testFile = (CmsFile) filesInFolder.get(3);
String fileName = testFile.getName();
int lengthOfContents = testFile.getContents().length;

//returns an empty string, should be a string with the contents of
the file; also tried it without "UTF-8" encoding
String contentsInString = new String(testFile.getContents(),
"UTF-8");
//empty result
System.out.println(contentsInString);
</snip>

I have no idea why it does not work. I transformed several byte
arrays to string in other programs with the same solution without any
problems.

Maybe someone can help me or show me another solution to get the
content of a file.

I cannot use the CmsJspActionElement in some way in a normal class
(not a jsp and no servlet) or is there any way?

Thanks in advance,
Jan



More information about the opencms-dev mailing list