Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



BufferedReader can not find file

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 27-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
BufferedReader can not find file

Hello,
I have a package where I placed the files. I think in my code the BufferedReader can not find the file.
Code:
BufferedReader in = new BufferedReader(new FileReader("path of the directory"));
it not work: File Not Found!
Code:
 getClass.getResource("path of the directory ")
does not work either since the last review you a URL and the FileReader constructor accepts no URL. Any idea hwy it is not recognizing the file.
Reply With Quote
  #2  
Old 27-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: BufferedReader can not find file

Hello,
Try the following method
Code:
getClass().getClassLoader().getRessourceAsStream("your directory");
That's how I load my resource files and it works very well! You will retrieve a InputStream by cons, and not a FileReader: I hope it does not bother you. In my opinion, you have trouble finding a Java method that enables you to open a resource located in a package with the File class or FileReader. If you compiles your project jar, your resources are encapsulated in the jar, and File or FileReader not work naturally (lisentpas these classes inside a jar). It is therefore natural to not open a resource by a FileReader in my opinion. By cons, getRessourceAsStream () opens your resource without problem inside a jar.
Reply With Quote
  #3  
Old 27-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
Re: BufferedReader can not find file

Hello,
I need to try the following in your code
Code:
getClass.getResources ( "your path to the directory ");
You've got to be in the root of the jar archive. I guess you are clear with the input output concepts which are used in java. If you are not the you can visit the Sun's official site and there you can find a good documentation on the java.io package with simple example which are better for understanding.
Reply With Quote
  #4  
Old 27-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: BufferedReader can not find file

Hello,
Even i am facing a similar kind of a problem.
I have tried these , but unfortunately they do not work for me
1) because it returns a URL;
2) because even if I do
Code:
getClass.getResources ( "path to the directory"). toString ();
Reply With Quote
  #5  
Old 27-02-2010
Member
 
Join Date: Nov 2009
Posts: 359
Re: BufferedReader can not find file

Hello,
See if the following code works for you.
Here it is
Code:
        BufferedReader buff = null;
        BufferedWriter buffout = null;
        String s = null;
        try {
            buffout = new BufferedWriter (new FileWriter("tmp.tmp"));
            InputStream input = getClass().getResourceAsStream("path to the direcotyr ");
            int i;
            while ((i = input.read())!=-1)
                buffout.write(i);
            buffout.close();
            input.close();
            buff = new BufferedReader(new FileReader("tmp.tmp"));
            / **
               treatment on the binary
            * /
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        File f = new File("tmp.tmp");
        f.delete();
Reply With Quote
  #6  
Old 27-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: BufferedReader can not find file

Hello,
Here is the more generalized form of the code.
Code:
try {
	BufferedReader in = new BufferedReader(
		new InputStreamReader( getClass().getResourceAsStream("path to the direcotyr ") ) );
	try {
		/ **
treatment on the binary
* /
 
	} finally {
		in.close();
	}
} catch (IOException e) {
	e.printStackTrace();
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "BufferedReader can not find file"
Thread Thread Starter Forum Replies Last Post
Closing the bufferedreader Vodka Software Development 5 27-01-2010 12:21 PM
Problems with BufferedReader Remedy Software Development 5 25-01-2010 11:42 AM
Problem in BufferedReader SoftWore Software Development 3 02-12-2009 01:18 PM
find file containing text inside the file pacinitaly Vista Help 0 22-10-2009 07:42 AM
Office 2003 Set-up stalls: Says it can't find file -- but file is really there! Clueless in Seattle Window 2000 Help 2 03-06-2006 06:35 PM


All times are GMT +5.5. The time now is 03:40 AM.