cancel
Showing results for 
Search instead for 
Did you mean: 

how to read CSV file from alfresco repository

Isha
Champ in-the-making
Champ in-the-making

Hi,

I have stored CSV file in repository using bulk import .Now I want to read that CSV file in my java class .Also I have noderef of the particular CSV file.

Kindly suggest API's/Sample code to read CSV file in alfresco .

Thanks 

Isha 

2 REPLIES 2

sanjaybandhaniya
Elite Collaborator
Elite Collaborator

abhinavmishra14
World-Class Innovator
World-Class Innovator

Hi @Isha 

Use NodeService (NodeService Docs) to double check the node existance and get the name of csv file for creating a temp file which you will use during the processig of your program and at the end delete the temp csv file.


Use ContentService (ContentSerice Docs) to get the instance of ContentReader (as mentioned by Sanjay) for the csv nodeRef.

Pseudo Code:

File tempCSVFile = null;
try {
	if (csvNodeRef != null && nodeService.exists(csvNodeRef)) {
	    final String csvFileName = (String) nodeService.getProperty(csvNodeRef, ContentModel.PROP_NAME); //get the file name from node.

		//create temp file in temp directory
		final File tempDir = new File(System.getProperty("java.io.tmpdir"));
		tempCSVFile = File.createTempFile(csvFileName, tempDir);
							
		final ContentReader reader = contentService.getReader(csvNodeRef, ContentModel.PROP_CONTENT);
		if (reader.getSize() > 0) {
			reader.getContent(tempCSVFile);
		}
		//Now you have csv temp file, you can read it as file input stream and process the stream
		final InputStream inStreamJson = Files.newInputStream(Paths.get(tempCSVFile));
		//Process the stream as needed.
		
	}
} catch(InvalidNodeRefException | ContentIOException nodeEx) {
  //log error appropriately 
} catch(AlfrescoRuntimeException alfEx) {
  //log error appropriately 
} catch(RuntimeException runEx) {
  //log error appropriately 
} catch(Exception excp) {
  //log error appropriately 
} finally {//delete temp csv file.
	if (tempCSVFile != null && tempCSVFile.exists()) {
		tempCSVFile.delete();
	}
}
~Abhinav
(ACSCE, AWS SAA, Azure Admin)