• 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">
                  <value>c:/rad/translator/src/resources/sampledata/member.lst</value>
            </constructor-arg>            
      </bean>      

I am having 'File not found error". Could anyone suggest me where i went wrong
0
cutie_smily
Asked:
cutie_smily
  • 2
1 Solution
 
sunray_2003Commented:
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\\...
0
 
ragerinoCommented:
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"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
                  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">
                  <value>c:/temp/test.txt</value>
            </constructor-arg>
      </bean>

</beans>

this is the output:
Cp1252

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");
		
		System.out.println(reader.getEncoding());
		reader.close();
 
	}
 
}

Open in new window

0
 
sunray_2003Commented:
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.
0

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