It's all about the answers!

Ask a question

Updating a file using java API

K M (38325050) | asked Apr 26 '10, 3:41 p.m.
Is there an example out there for:

Getting the file contents of a file in a component.
Updating the contents of the file.
Delever the changes.

Using the Java API

7 answers

permanent link
K M (38325050) | answered Aug 13 '12, 7:49 a.m.
I never saw a way to just insert into the file, I always red the whole file change what I want and then put it back.

permanent link
Jean-Michel Lemieux (2.5k11) | answered Apr 30 '10, 9:59 a.m.
Once you've setup your development environment following the instructions in, there are snippets that show how to deliver.

If you take a look the project in the SDK, you'll see several tasks and checkin.deliver.add files.


permanent link
K M (38325050) | answered Apr 30 '10, 12:11 p.m.
I see one example adding a file...........

None updating one........

can you send me the code sample........thank you

permanent link
K M (38325050) | answered May 03 '10, 2:08 p.m.
List components = workspace.getComponents();
for (Iterator a = components.iterator(); a.hasNext();) {
IComponentHandle component = (IComponentHandle);
IComponent comp = (IComponent)repo.itemManager().fetchCompleteItem(component, IItemManager.DEFAULT, MONITOR);

// The root folder is created when the component is created.
// add a folder called 'project' to the workspace
IChangeSetHandle cs1 = workspace.createChangeSet(component, MONITOR);
IFolder rootFolder = (IFolder) workspace.configuration(component).rootFolderHandle(MONITOR);

// Find file handle
IConfiguration iconfig = (IConfiguration) workspace.configuration(component);
IFolderHandle root = iconfig.rootFolderHandle(MONITOR);
IVersionableHandle filePathHandle = iconfig.resolvePath(root,nameSegments, MONITOR);

// Check if file found
if (filePathHandle == null) {
throw new TeamRepositoryException("Could not find file " + nameSegments);

// Fetch file complete item
IVersionable filePath = (IVersionable) iconfig.fetchCompleteItem(filePathHandle, MONITOR);
if (filePath.hasFullState()) {
file = (IFileItem) filePath.getFullState();
} else {
throw new TeamRepositoryException("Could not find file " + nameSegments + " item");

// Get file content
IFileContent content = file.getContent();
contentManager.retrieveContent(file, content, outputStream, MONITOR);

file = (IFileItem) file.getWorkingCopy();

VersionedContentManagerByteArrayInputStreamPovider jack =
new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes());

/* IContentService contentService = getService(IContentService.class);
byte[] notesTextBytes;

notesTextBytes = notes.getBytes(CONTENT_ENCODING_UTF8);
ByteArrayInputStream inputStream = new ByteArrayInputStream(
CONTENT_ENCODING_UTF8, inputStream, notesTextBytes.length);
IFileContent storedContent = contentManager.storeContent(content.getCharacterEncoding(),content.getLineDelimiter(),
new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes()), content.getHash(), MONITOR);

file.setFileTimestamp(new Date());

workspace.commit(cs1,Collections.singletonList(workspace.configurationOpFactory().save(file)), MONITOR);

// deliver the changes to the stream
IChangeHistorySyncReport sync =
workspace.compareTo(stream, WorkspaceComparisonFlags.CHANGE_SET_COMPARISON_ONLY,Collections.EMPTY_LIST, MONITOR);
workspace.deliver(stream, sync, Collections.EMPTY_LIST,
sync.outgoingChangeSets(component), MONITOR);
MONITOR.subTask("Created changes and delivered to " + stream.getName());

permanent link
Michael Wang (1125) | answered Aug 13 '12, 5:56 a.m.
Do you know how can I modify the existing file such as insert one line at the begining or end of the file.
Because I do not want to update the whole file.
I can not find the such api, Can you tell me how to do?
Thank you very much.


permanent link
John Camelon (1.7k14) | answered Aug 13 '12, 8:55 a.m.
K M is correct, you have to upload the entire content.  

permanent link
Michael Wang (1125) | answered Aug 13 '12, 11:06 a.m.
 That is to say I could not just modify a line one time, and each time I have to write the whole file and upload?
but I think it's really not a good method. Is there any good ways? Can I use the iostream to modify the file locally and 
then upload it to the server? 

Your answer

Register or to post your answer.