It's all about the answers!

Ask a question

How to change character encoding for a file using the java api.


K M (38325051) | asked Jul 26 '13, 10:49 a.m.
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



permanent link
Tim Mok (6.6k38) | answered Jul 26 '13, 11:24 a.m.
JAZZ DEVELOPER
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
K M commented Jul 26 '13, 12:25 p.m. | edited Jul 26 '13, 2:10 p.m.

Tim Mok commented Jul 26 '13, 2:13 p.m.
JAZZ DEVELOPER

The javadoc for IFileContentManager#storeContent() doesn't state that it stores the encoding. It uses the encoding to perform validation/translation of line endings.

FYI, you can provide a comment to answer instead of responding to an answer with another answer. This helps categorize responses for other users to quickly identify responses that are answers.


permanent link
K M (38325051) | answered Jul 29 '13, 8:41 a.m.
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);




permanent link
K M (38325051) | answered Jul 29 '13, 8:43 a.m.
           IFileContent newcontent = contentManager.storeContent(<new encoding goes here>,content.getLineDelimiter(),
                    new VersionedContentManagerByteArrayInputStreamPovider(FileUtils.readFileToByteArray(tmpfile)),null, MONITOR); 

Your answer


Register or to post 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.