Updating a file using java API
![]()
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
![]()
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.
|
![]()
Once you've setup your development environment following the instructions in https://jazz.net/wiki/bin/view/Main/RTCSDK20_DevelopmentEnvironmentSetup, there are snippets that show how to deliver.
If you take a look the com.ibm.team.filesystem.client.tests.workload project in the SDK, you'll see several tasks and checkin.deliver.add files. Jean-Michel |
![]()
I see one example adding a file...........
None updating one........ can you send me the code sample........thank you |
![]()
List components = workspace.getComponents();
for (Iterator a = components.iterator(); a.hasNext();) { IComponentHandle component = (IComponentHandle) a.next(); 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( notesTextBytes); contentService.storeContent(IContent.CONTENT_TYPE_TEXT, CONTENT_ENCODING_UTF8, inputStream, notesTextBytes.length); */ IFileContent storedContent = contentManager.storeContent(content.getCharacterEncoding(),content.getLineDelimiter(), new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes()), content.getHash(), MONITOR); file.setContent(storedContent); 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()); } |
![]()
Hello,
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. BR, Michael |
![]()
K M is correct, you have to upload the entire content.
|
![]()
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?
|