• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

In a cofiguration file i am specifying a bean and its constructor. but i am getting FILE NOT FOUND EXCEPTION

<bean name="FileReader" class="java.io.FileReader">
            <constructor-arg index="0">

I am having 'File not found error". Could anyone suggest me where i went wrong
  • 2
1 Solution
You can verify whether the file exists in the location  c:/rad/translator/src/resources/sampledata/member.lst mentioned in your configuration file. Btw, is it member.lst or txt? Also you can escape character to the file pah C:\\rad\\...
i've tested it and it worked properly.
i used this applicationContext.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

      <bean name="FileReader" class="java.io.FileReader">
            <constructor-arg index="0">


this is the output:

when i changed the path to the file to a non-existing file i got this Exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FileReader' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.io.FileReader]: Constructor threw exception; nested exception is java.io.FileNotFoundException: c:\temp\testx.txt

below is the code of my testapp
import java.io.FileReader;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	 * @param args
	 * @throws IOException 
	public static void main(String[] args) throws IOException {
		ApplicationContext appCtx=new ClassPathXmlApplicationContext("applicationContext.xml");
		FileReader reader=(FileReader)appCtx.getBean("FileReader");

Open in new window

Your application is working as epected. When a file reader cannot find a file, it throws an Exception. And if the file is present it reads and processes it properly. There is no problem that I could see in your code.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now