komlaaa
asked on
java.io.FileNotFoundException
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.FileNotFoundExcept ion: cdDdatabase.txt (The system cannot find the file specified)
================= SOURCE ===============
try
{
BufferedReader infile = new BufferedReader
( new FileReader("cdDdatabase.tx t"));
while( ( line = infile.readLine()) != EOS )
{
in the same directory as the source code. Why am i having this error message:
Unexpected IO erro: java.io.FileNotFoundExcept
================= SOURCE ===============
try
{
BufferedReader infile = new BufferedReader
( new FileReader("cdDdatabase.tx
while( ( line = infile.readLine()) != EOS )
{
the file needs to be in the same directory you are running it from.
ASKER
It is in the same directory as i am running from
then it should find it, what command do you use to run it?
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.ja va");
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.ja va");
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.i n);
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.
FileReader file = new FileReader("HelloWorld2.ja
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.ja
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.i
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.
Post ur code and the directory structure u kept.....i'll check it out
ASKER
========================== === 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("Unexpect ed IO erro: " + e);
}
}
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
CDs.add(typeOfCd);
}
if( 1 == super.getType() )
{
typeOfCd = new CompilationCd();
((CompilationCd)typeOfCd ).read(fields, infile);
CDs.add(typeOfCd);
}
CDCollections.put(fields[1
CdCategories.add(fields[1]
}
infile.close();
}
catch(IOException e)
{
System.err.print("Unexpect
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
my bad splitting the point, could the page editor give , the assisted answer points to dungla ?
Use
System.out.println(new File("cdDdatabase.txt").ge tAbsoluteP ath());
to see what's the file absolute path and see does it exists on that path.
Are you writing java applet?
System.out.println(new File("cdDdatabase.txt").ge
to see what's the file absolute path and see does it exists on that path.
Are you writing java applet?
Haha .... so late ;-)
ASKER
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...?
see...?