04-29-2020 05:47 AM
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
04-29-2020 06:11 AM
Hi,
You can use ContentReader service read content of file.
04-29-2020 08:46 AM
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(); } }
Explore our Alfresco products with the links below. Use labels to filter content by product module.