Results 1 to 6 of 6

Thread: BufferedReader can not find file

  1. #1
    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.

  2. #2
    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.

  3. #3
    Join Date
    Nov 2009
    Posts
    518

    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.

  4. #4
    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 ();

  5. #5
    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();

  6. #6
    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();
    }

Similar Threads

  1. Replies: 18
    Last Post: 16-06-2012, 06:36 AM
  2. Replies: 5
    Last Post: 03-03-2011, 11:30 PM
  3. Closing the bufferedreader
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 12:21 PM
  4. Problems with BufferedReader
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 11:42 AM
  5. Problem in BufferedReader
    By SoftWore in forum Software Development
    Replies: 3
    Last Post: 02-12-2009, 01:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,401,425.82739 seconds with 16 queries