Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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

Avatar

Level 5

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

5 Replies

Avatar

Level 5

@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

Avatar

Correct answer by
Community Advisor

@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

Avatar

Level 5

@arunpatidar thank you for the solution , I am now able to access the file in dam , I want to update data with my script in the mentioned file , can you tell me how to do  it?

thank you