(Simple question) Why I failed to read from an input file?

S

Shawn

Hi,

I believe I fully understand how to do input/output and I have never run
into problems. But I don't know why this time I cannot make it work. It
is very simple: a text file called "d_array.txt", located in the SAME
folder. But the error says "The system cannot find the file specified".

try
{
BufferedReader inputStream = new BufferedReader(new
FileReader("d_array.txt"));
String line = null;
line = inputStream.readLine().trim();
while ( line != null )
{
this.D.add(Double.valueOf(line));
line = inputStream.readLine().trim();
}
inputStream.close();
}
catch block



I have played the path to the file like following, neither worked:

new FileReader("./d_array.txt");
new FileReader("D:/programming/d_array.txt");
new FileReader("D:\\programming\\d_array.txt");

I am using Eclipse. This is the only thing different from my previous
experience.

Thank you for your help.
 
I

Ingo R. Homann

Hi Shawn,
Hi,

I believe I fully understand how to do input/output and I have never run
into problems. But I don't know why this time I cannot make it work. It
is very simple: a text file called "d_array.txt", located in the SAME
folder.

"The SAME folder" as what?

The class-files? Note that the location of the class-files does not have
anything to do with the 'current directory' (".") which you probably
mean. (*)
new FileReader("d_array.txt"));
new FileReader("./d_array.txt");
new FileReader("D:/programming/d_array.txt");
new FileReader("D:\\programming\\d_array.txt");

This should all work, if "D:/programming/" is your current directory.

Your error must be elsewhere!

Ciao,
Ingo

(*) Note that there are methods like Class.getResourceAsStream() and
ClassLoader.getResourceAsStream()!
 
O

Oliver Wong

Shawn said:
Hi,

I believe I fully understand how to do input/output and I have never run
into problems. But I don't know why this time I cannot make it work. It
is very simple: a text file called "d_array.txt", located in the SAME
folder. But the error says "The system cannot find the file specified".

try
{
BufferedReader inputStream = new BufferedReader(new
FileReader("d_array.txt"));
String line = null;
line = inputStream.readLine().trim();
while ( line != null )
{
this.D.add(Double.valueOf(line));
line = inputStream.readLine().trim();
}
inputStream.close();
}
catch block



I have played the path to the file like following, neither worked:

new FileReader("./d_array.txt");
new FileReader("D:/programming/d_array.txt");
new FileReader("D:\\programming\\d_array.txt");

Are you sure your d_array.txt file is located in your D:\programming
directory?

- Oliver
 
S

Shawn

Ingo said:
Hi Shawn,


"The SAME folder" as what?

The class-files? Note that the location of the class-files does not have
anything to do with the 'current directory' (".") which you probably
mean. (*)


This should all work, if "D:/programming/" is your current directory.

Your error must be elsewhere!

Ciao,
Ingo

(*) Note that there are methods like Class.getResourceAsStream() and
ClassLoader.getResourceAsStream()!

Sorry. I have made it work. Somehow, the Eclipse required me to use the
full path (even though my text input file is located in the same
directory as the java file). Soshould work. It didn't before, because of something else.

Thank you for your help.
 
S

Shawn

Oliver said:
Are you sure your d_array.txt file is located in your D:\programming
directory?

- Oliver

Thank you. Yes, I have replied previously. That,

should have worked. It failed due to some other error.
 
I

Ingo R. Homann

Hi,
Somehow, the Eclipse required me to use the
full path (even though my text input file is located in the same
directory as the java file).

Note that the location of the source-files is not available at runtime!

Ciao,
Ingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top