is there any way to read and write data from csv file using groovy script from DAM in AEM? | Community
Skip to main content
Level 4
October 10, 2022
Solved

is there any way to read and write data from csv file using groovy script from DAM in AEM?

  • October 10, 2022
  • 1 reply
  • 4092 views

I have a csv file placed in dam , I want to write a groovy script to update the csv file with the new data,

the data I am able to fetch with the script only thing that is require is to push it into csv file

 

e.g : how to push "demo data " to csv file

 

Please help me out here, what could be the possible solution for this 

 

Thank you

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

@rahul234dabas 
Use Asset API to get the binray data of csv, IO API will not work here.

Resource rs = resourceResolver.getResource(path);
Asset asset = rs.adaptTo(Asset.class);

// We have the inputstream
InputStream data = asset.getOriginal().getStream();

1 reply

arunpatidar
Community Advisor
Community Advisor
October 10, 2022
Level 4
October 11, 2022

@arunpatidar  I am getting file not found error even when I have placed the file at the same place 

 

:  

filePath = "/content/dam/demo/output.csv";

File file = new File(filePath);
def rows = file.readLines().tail()*.split(',');                                // getting exception here as file not found exception
println "the file is : "+ rows;

 

 

 

The file is present in the location and is published also  not sure why it's not picking it up

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
October 11, 2022

@rahul234dabas 
Use Asset API to get the binray data of csv, IO API will not work here.

Resource rs = resourceResolver.getResource(path);
Asset asset = rs.adaptTo(Asset.class);

// We have the inputstream
InputStream data = asset.getOriginal().getStream();
Arun Patidar