2

I have a text file with name data.txt which includes Customer's details. I need to send a Msg to each Customer using apache velocity. My file look like this

enter code here
First_Name, City, ... 
Rakesh,  Hyderabad,...
Jack,   berlin,... 
....,   ...., ...

like this 10000 records are their.. i don't know apache velocity very much.. i am unable to load my file in velocity. my java file

public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");

VelocityContext ctx = new VelocityContext();

Writer writer = new StringWriter();
t.merge(ctx, writer);

System.out.println(writer);

VM file

#if ($City == "Hyderabad")
  Hi $first_Name welcome to Hyderabad.
#elseif ($City == "Berling")
  Hi $First_Name welcome to Berlin
#elseif ($City == "Mumbai")
  Hi $First_Name welcome to Mumbai
#else
 Please contact Us
#end
enter code here
the output should be like this..
Hi Rakesh welcome to Hyderabad.

Someone please suggest me how to load data and use in velocity. If Person belongs to some city then the name should be taken dynamically from a file. Please Someone help me out with this.. just help me including my file in velocity.

  • You can add it in the map and access – soorapadman Sep 20 '18 at 09:30
  • @manfromnowhere Will you please help me with some code pls – farhana fatima Sep 20 '18 at 09:38
  • I need to understand your whole requirement . You mean to say 10k user you want to send msg? By reading the text file? – soorapadman Sep 20 '18 at 09:47
  • @manfromnowhere i am getting an exception like this SEVERE: ResourceManager : unable to find resource './src/apachefilepath.vm' in any resource loader. Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource './src/apachefilepath.vm'.. – farhana fatima Sep 20 '18 at 09:52
  • This means Your vm file not able to find . – soorapadman Sep 20 '18 at 09:54
  • Please check my answer ;https://stackoverflow.com/questions/44405897/org-apache-velocity-exception-resourcenotfoundexception-unable-to-find-resource/44409087#44409087 – soorapadman Sep 20 '18 at 09:55
  • Along with that You have to add this code snippet also to generate the output `VelocityContext ctx = new VelocityContext(); ctx.put("City", "Hyderabad");/ / Hyderabad should be dynamic ctx.put("first_name", "Rakesh");// This is also` – soorapadman Sep 20 '18 at 09:58
  • @manfromnowhere yes.. vm file is not able to find.. I have added but also it throwing an exception – farhana fatima Sep 20 '18 at 10:03
  • where did you keep the vm file ? Could you please keep it under `resources/velocity` folder? – soorapadman Sep 20 '18 at 10:06
  • 1
    @its working but i need to use my text file(data.txt) which contains 10000 records of different customer. what name and city i am giving in context . its giving output to one customer.. i need to give to all customers – farhana fatima Sep 20 '18 at 10:36
  • @Yes you must loop it one by one. – soorapadman Sep 20 '18 at 10:40
  • @manfromnowhere when i am using #include("D:\\temp\\data.txt") in vm file its throwing an exception like unable to load a file.. how can i used my data.txt file velocity is my main problem.. its a task for me.. and i am very new to java and velocity :( – farhana fatima Sep 20 '18 at 10:42
  • Did you put your `text file` under velocity folder ? – soorapadman Sep 20 '18 at 10:48
  • I think you can't use it like `#include("D:\\temp\\data.txt")` instead you can put under velocity folder and try `#include("data.txt")` – soorapadman Sep 20 '18 at 10:50

0 Answers0