Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Shawn - 21 Nov 2006 15:46 GMT
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.
Ingo R. Homann - 21 Nov 2006 16:00 GMT
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()!
Shawn - 21 Nov 2006 16:16 GMT
> Hi Shawn,
>
[quoted text clipped - 25 lines]
> (*) 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). So
>> new FileReader("D:\\programming\\d_array.txt");
should work. It didn't before, because of something else.

Thank you for your help.
Ingo R. Homann - 22 Nov 2006 09:38 GMT
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
Oliver Wong - 21 Nov 2006 16:13 GMT
> Hi,
>
[quoted text clipped - 23 lines]
> 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
Shawn - 21 Nov 2006 16:26 GMT
>> I have played the path to the file like following, neither worked:
>>
[quoted text clipped - 6 lines]
>
>     - Oliver

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

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

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.