cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a file and its content programtically

yasir_mahmood
Champ in-the-making
Champ in-the-making
Hi
I have studied the alfresco sample to write and read the contents of file through sample ContentReadAndWrite in Alfreco SDK. There is statement
Reference contentReference = ContentReadAndWrite.createNewContent(contentService, strFileName, strContents);
This statement writes the new contents in the file. But I just need to read the contents.
I need to make a method that takes filename as input and outputs the contents of that file.

How to do that? Any Help?

Regards
Yasir Mahmood
5 REPLIES 5

dgenard
Champ on-the-rise
Champ on-the-rise
ContentService makes it easy. Example :

ContentReader contentReader = contentService.getReader(sourceNodeRef, ContentModel.PROP_CONTENT);
String content = contentReader.getContentString();
Denis

yasir_mahmood
Champ in-the-making
Champ in-the-making
Thanks!
How to get 'sourceNodeRef' because method has only FileName as input.
how to get sourceNodeRef ?
Please do let me know.
Thanks

dgenard
Champ on-the-rise
Champ on-the-rise
Look at other services like NodeService or SearchService which will provide methods to find a NodeRef.
Details on http://wiki.alfresco.com/wiki/Java_API

Denis

yasir_mahmood
Champ in-the-making
Champ in-the-making
I tried my best to find out how to use contentReader to read the contents of a file in the Alfreco repository. But I could not get help. I am very new to alfreco and working on very first opensource projecct. I request if some can help to read the content of file, given that we know the fileName.

Regards

Yasir Mahmood

yasir_mahmood
Champ in-the-making
Champ in-the-making
I have done it. Here is the way…
Reference newContentReference = new Reference(STORE,nodeId,null);
                    
             ContentServiceSoapBindingStub cs = WebServiceFactory.getContentService();
             Content[] readResult = cs.read(new Predicate(new Reference[]{newContentReference},STORE,null), Constants.PROP_CONTENT);
             Content content = readResult[0];
                
                   System.out.println(ContentUtils.getContentAsString(content));

Thanks to all those who participate in this post for help.

Regards
Yasir Mahmood