Note that there are some explanatory texts on larger screens.

plurals
  1. POmethod in spring to read txt file
    text
    copied!<p>I am having a requirement wherein I need to read the contents of a text file through spring framework. For this purpose I made a method in my service implementation class as below- </p> <pre><code>public String readFile(File file) </code></pre> <p>This method will take the file name as input and read the file.</p> <p>I was writing the code in XML for spring as below-</p> <pre><code>&lt;bean id="fstream" class="java.io.FileInputStream"&gt; &lt;constructor-arg value="C:/text.txt" /&gt; &lt;/bean&gt; &lt;bean id="in" class="java.io.DataInputStream"&gt; &lt;constructor-arg ref="fstream"/&gt; &lt;/bean&gt; &lt;bean id="isr" class="java.io.InputStreamReader"&gt; &lt;constructor-arg ref="in"/&gt; &lt;/bean&gt; &lt;bean id="br" class="java.io.BufferedReader"&gt; &lt;constructor-arg ref="isr"/&gt; &lt;/bean&gt; </code></pre> <p>Following code goes in my method-</p> <pre><code>public String readFile(File file) { String line = null; String content = ""; try { ApplicationContext context = new ClassPathXmlApplicationContext("FileDBJob.xml"); BufferedReader br = (BufferedReader) context.getBean("br"); while((line = br.readLine())!=null) content = content.concat(line); } catch (Exception e) { e.printStackTrace(); } return content; } </code></pre> <p>But here the issue is that i need to hardcode the file name in XML, so there is no use of file parameter.</p> <p>Kindly help in finding the solution. As I am new to spring and trying my hands with it so it may be possible that I am missing something. Any help would be of great help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload