?
Solved

java.io.FileNotFoundException

Posted on 2004-11-03
13
Medium Priority
?
313 Views
Last Modified: 2008-03-17
Below is how i connect to a file and trying to read from it. the file is
in the same directory as the source code. Why am i having this error message:
Unexpected IO erro: java.io.FileNotFoundException: cdDdatabase.txt (The system cannot find the file specified)

================= SOURCE ===============
try
        {
             BufferedReader infile = new BufferedReader
                                         ( new FileReader("cdDdatabase.txt"));
             
            while( ( line = infile.readLine()) != EOS )
            {
0
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12489526
the file needs to be in the same directory you are running it from.
0
 

Author Comment

by:komlaaa
ID: 12489550
It is in the same directory as i am running from
0
 
LVL 92

Expert Comment

by:objects
ID: 12489555
then it should find it, what command do you use to run it?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:vikraman_b
ID: 12489557
Before we can read from or write to a file we must first open the file. To open a file for reading we use the constructor contained FileReader class:

FileReader file = new FileReader("HelloWorld2.java");

This creates a stream object, called file, to read from a file called HelloWorld2.java (Table 1) contained in the current directory. If the named file cannot be opened a FileNotFoundException is generated. Consequently, whereas previously we declared instances of the InputStreamReader class as class instances we must now declare them as local instances within a method. Having done this we can now use the file object to create an instance of the BufferedReader class as before:

FileReader file          = new FileReader("HelloWorld2.java");
BufferedReader fileInput = new BufferedReader(file);

Note the similarity between this and the declarations used in the code given in Table 2:

 
InputStreamReader input         = new InputStreamReader(System.in);
BufferedReader    keyboardInput = new BufferedReader(input);

The BufferedReader constructor requires its argument to be an instance of the class Reader (or a sub-class of the class Reader), both the InputStreamReader and the FileReader classes are sub-callses of Reader and will therefore suffice. A class diagram illustrating the connections between these different classes is presented in Figure 1.

0
 
LVL 3

Expert Comment

by:vikraman_b
ID: 12489564
Post ur code and the directory structure u kept.....i'll check it out
0
 

Author Comment

by:komlaaa
ID: 12489615
============================= THE CODE =========================

 public void read()
    {
           CDCollections = new HashMap();
     
        String line;
        try
        {
             BufferedReader infile = new BufferedReader
                                         ( new FileReader("database.txt"));
             
            while( ( line = infile.readLine()) != EOS )
            {
                String fields[] = line.split("\\|");
                if( 0 == Integer.parseInt(fields[1]))
                {
                   typeOfCd = new Cd();
                   ((Cd)typeOfCd).read(fields, infile);
                   CDs.add(typeOfCd);
                }
               
               if( 1 == super.getType() )
               {
                   typeOfCd = new CompilationCd();
                   ((CompilationCd)typeOfCd ).read(fields, infile);
                   CDs.add(typeOfCd);
               }
              CDCollections.put(fields[1], typeOfCd );
              CdCategories.add(fields[1]);
             }
               infile.close();
         }
        catch(IOException e)
        {
            System.err.print("Unexpected IO erro: " + e);
        }
    }
   
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 12489632
the codes fine, you just need to ensure the file database.txt is in the same directory as you are tunning the applciation from.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12489636
otherwise include the full path to the file.
eg. if the file is in /database/files/database.txt then use:

             BufferedReader infile = new BufferedReader
                                         ( new FileReader("/database/files/database.txt"));
0
 
LVL 13

Expert Comment

by:dungla
ID: 12489677
the file needs to be in the same directory you are running it from, for example, i have a project (create by NetBean) name MyApp, source file placed in MyApp/src/mypackage/source.java

and text file should be in MyApp/Text.txt

BufferedReader infile = new BufferedReader(new FileReader("Text.txt")); //will work correctly.

Other wise, you need to put fullpath of files.
0
 

Author Comment

by:komlaaa
ID: 12489874
my bad splitting the point, could the page editor give , the assisted answer points to dungla ?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 12490087
Use

System.out.println(new File("cdDdatabase.txt").getAbsolutePath());

to see what's the file absolute path and see does it exists on that path.

Are you writing java applet?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 12490107
Haha .... so late ;-)
0
 

Author Comment

by:komlaaa
ID: 12501579
I guess i just find a bug in this EE page implementation: The software shouldn't let me split point among the same Experts
see...?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question