Unable to update the content of file content | Community
Skip to main content
Mario248
Level 7
March 14, 2024
Solved

Unable to update the content of file content

  • March 14, 2024
  • 3 replies
  • 778 views
I am programmatically creating a hello.txt file and it works fine as expected but it is not working when I try to update the same file again.
For example, I have "hello world" in hello.txt and now I am trying to update this to "hello world 1" but it is not updating.
 

 

 

public void createFile() { try { // Add the text file node Node fileNode = projectNode.addNode("hello.txt", "nt:file"); Node contentNode = fileNode.addNode("jcr:content", "nt:resource"); // Set the jcr:data property with the file content contentNode.setProperty("jcr:data", "hello world 1"); // Save the session to persist changes session.save(); } catch (RepositoryException e) { e.printStackTrace(); } finally { if (resolver.isLive()) { resolver.close(); } } }

 

 

I also tried to remove and set the content as below but it is not working,

 

 

 

if (fileNode.hasNode("jcr:content")) { fileNode.getNode("jcr:content").remove(); } Node contentNode = fileNode.addNode("jcr:content", "nt:resource");

 

 

 

Is there any way to handle this case? But still it is not working.

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 BrianKasingli

Try this

This method, createOrUpdateFile, will check if the file already exists. If it does, it retrieves the existing file node and updates its content. If the file doesn't exist, it creates a new file node and sets its content. This approach is versatile for both creating new files and editing existing ones within AEMaaCS, adhering to best practices for content management in AEM.

 

Remember to manage your session and projectNode correctly, ensuring they are properly initialized and closed to avoid resource leaks. Also, adjust the MIME type and encoding as necessary for your specific file content requirements.

 

 

import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; public class FileOperations { private Session session; private Node projectNode; // Constructor to set session and the project node public FileOperations(Session session, Node projectNode) { this.session = session; this.projectNode = projectNode; } public void createOrUpdateFile(String fileName, String content) { try { Node fileNode; // Check if the file node already exists if (projectNode.hasNode(fileName)) { // If file exists, retrieve it fileNode = projectNode.getNode(fileName); } else { // If file does not exist, create it fileNode = projectNode.addNode(fileName, "nt:file"); fileNode.addNode("jcr:content", "nt:resource"); } // Retrieve or create the jcr:content node Node contentNode = fileNode.getNode("jcr:content"); // Update the content contentNode.setProperty("jcr:data", content); // Optionally, set the mime type and encoding contentNode.setProperty("jcr:mimeType", "text/plain"); contentNode.setProperty("jcr:encoding", "UTF-8"); // Save the session to persist changes session.save(); } catch (RepositoryException e) { e.printStackTrace(); } finally { if (session.isLive()) { session.logout(); } } } }

 

 

 

3 replies

Raja_Reddy
Community Advisor
Community Advisor
March 14, 2024

Hi @mario248 

 it seems like you are creating a new file node with the content "hello world 1" every time the createFile() method is called. This means that if the file already exists, it will be overwritten with the new content.

To update the content of an existing file, you need to retrieve the existing file node and update its content instead of creating a new file node. 

public void updateFile() { try { // Get the file node Node fileNode = projectNode.getNode("hello.txt"); // Get the jcr:content node Node contentNode = fileNode.getNode("jcr:content"); // Set the jcr:data property with the new file content contentNode.setProperty("jcr:data", "hello world 1"); // Save the session to persist changes session.save(); } catch (RepositoryException e) { e.printStackTrace(); } finally { if (resolver.isLive()) { resolver.close(); } } }

In this updated code, we first check if the file node already exists using projectNode.hasNode("hello.txt"). If it exists, we retrieve the existing file node and update its content. If it doesn't exist, you can handle that case accordingly.
 

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
March 14, 2024

Try this

This method, createOrUpdateFile, will check if the file already exists. If it does, it retrieves the existing file node and updates its content. If the file doesn't exist, it creates a new file node and sets its content. This approach is versatile for both creating new files and editing existing ones within AEMaaCS, adhering to best practices for content management in AEM.

 

Remember to manage your session and projectNode correctly, ensuring they are properly initialized and closed to avoid resource leaks. Also, adjust the MIME type and encoding as necessary for your specific file content requirements.

 

 

import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; public class FileOperations { private Session session; private Node projectNode; // Constructor to set session and the project node public FileOperations(Session session, Node projectNode) { this.session = session; this.projectNode = projectNode; } public void createOrUpdateFile(String fileName, String content) { try { Node fileNode; // Check if the file node already exists if (projectNode.hasNode(fileName)) { // If file exists, retrieve it fileNode = projectNode.getNode(fileName); } else { // If file does not exist, create it fileNode = projectNode.addNode(fileName, "nt:file"); fileNode.addNode("jcr:content", "nt:resource"); } // Retrieve or create the jcr:content node Node contentNode = fileNode.getNode("jcr:content"); // Update the content contentNode.setProperty("jcr:data", content); // Optionally, set the mime type and encoding contentNode.setProperty("jcr:mimeType", "text/plain"); contentNode.setProperty("jcr:encoding", "UTF-8"); // Save the session to persist changes session.save(); } catch (RepositoryException e) { e.printStackTrace(); } finally { if (session.isLive()) { session.logout(); } } } }

 

 

 

Imran Khan
Community Advisor
Community Advisor
March 14, 2024

@mario248 please fing below code snippet to update text file by @debal_das 

AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);

String filename = "Some-Name"+".csv";

File csvFile = new File(filename);

FileWriter fileWriter = new FileWriter(csvFile);

for(String[] data : listdata){

  StringBuilder line = new StringBuilder();

   for (int i=0; i<data.length;i++){

      line.append("\"");

      line.append(data[i].replaceAll("\"","\"\""));

      line.append("\"");

      if(i!=data.length-1){

           line.append(',');

       }

  }

  line.append("\n");

fileWriter.write(line.toString());

}

fileWriter.close();

InputStream fileInputStream = new FileInputStream(csvFile);

assetManager.createAsset("/content/dam/debal/"+ filename,fileInputStream, null, true);

Here, listdata was an ArrayList which was containing search result. Please review this as a reference.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-add-txt-file-programmatically-in-aem-dam/m-p/452404