Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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

0 votes



7 answers

Permanent link
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.

1 vote


Permanent link
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

0 votes


Permanent link
I see one example adding a file...........

None updating one........

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

0 votes


Permanent link
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());
}

0 votes


Permanent link
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

0 votes


Permanent link
K M is correct, you have to upload the entire content.  

0 votes


Permanent link
 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? 

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details

Question asked: Apr 26 '10, 3:41 p.m.

Question was seen: 9,909 times

Last updated: Aug 13 '12, 11:06 a.m.

Confirmation Cancel Confirm