How to change character encoding for a file using the java api.
I have the file IFileItem file = (IFileItem) filePath.getFullState();
I can change a few attributes, but I can find nothing about changing the encoding. I can change the encoding using the gui, but I have not found a way to change it using the java api. |
3 answers
Text encoding isn't handled by source control. It's however the client is set to interpret the file. For Eclipse clients, there are preferences for the encoding that users can set for each type of file. Eclipse can also set the encoding per project. So the change you're making in the GUI isn't being shared with source control. If something is being shared, do you mean some other file property? The recorded properties are executable bit, mime type, and line delimiter.
Comments This sort of disagrees with you https://jazz.net/forum/questions/120592/contents-are-not-readable-after-programmatically-delivering-files-to-stream
The javadoc for IFileContentManager#storeContent() doesn't state that it stores the encoding. It uses the encoding to perform validation/translation of line endings.
|
To change the encoding I had to to read the file and restore it using
IFileContent content = file.getContent(); IFileContent newcontent = contentManager.storeContent(content.getCharacterEncoding(),content.getLineDelimiter(), new VersionedContentManagerByteArrayInputStreamPovider(FileUtils.readFileToByteArray(tmpfile)),null, MONITOR); // Set content to orginal file content file = (IFileItem) file.getWorkingCopy(); file.setContent(newcontent); |
IFileContent newcontent = contentManager.storeContent(<new encoding goes here>,content.getLineDelimiter(),
new VersionedContentManagerByteArrayInputStreamPovider(FileUtils.readFileToByteArray(tmpfile)),null, MONITOR); |
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.