Search code examples
javafilesap-commerce-cloud

Hybris rewrite existing file


I'm implementing service, that should store csv file in the Hybris backoffice, one file for one day. (and add one line when the specific process is done)

Is there any way how to rewrite that file, because only thing I'm still getting is new versions of the file. Is there any doc for this? I'm afraid there isn't any at help.hybris.com

File fileForCatalog = ... // I'm able to get that file from backoffice and add a line
Media mediaForFile = MediaManager.getInstance().createMedia(fileForCatalog.getName());
// creates new file 
mediaForFile.setFile(fileForCatalog);
mediaForFile.writeReplace(); // creates duplicate

Solution

  • I would suggest using medias with the MediaService.

    // Get media, f.e. by code
    MediaModel media = mediaService.getMedia("myMediaCode");
    InputStream oldInputStream = mediaService.getStreamFromMedia(media);
    // add your logic to add a line
    InputStream newInputStream = ...;
    mediaService.setStreamForMedia(media, newInputStream);
    

    Lookup Adding characters to beginning and end of InputStream in Java for examples how to add your line to the input stream.