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.
Solved! Go to Solution.
Views
Replies
Total Likes
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();
}
}
}
}
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.
Views
Replies
Total Likes
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();
}
}
}
}
@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.
Views
Likes
Replies
Views
Like
Replies