Solved

open a file

Posted on 1998-07-22
3
224 Views
Last Modified: 2010-03-30
how do you open a file in java when given a filename and directory information?
0
Comment
Question by:javalearner
[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
  • 2
3 Comments
 

Accepted Solution

by:
bjornh earned 50 total points
ID: 1227633
Try this to read and print a text file. Dont forget to define variables dir and filename.

try
{
    BufferedReader in = new BufferedReader(new FileReader(dir + filename));
    String s;
    while ((s = in.readLine()) != null)
    {      // Do something with s
    }
    in.close();
} catch(IOException e)
{      // Something went wrong, print error message
}
0
 

Author Comment

by:javalearner
ID: 1227634
i have four methods in my class:  open(), read(), write(), and close().
i just want to have a file open first.  whether to read or write from the file will be determined later, and the actual reading and writing is not done in open().
is this all i need to open a file:
        BufferedReader in = new BufferedReader(new FileReader(dir + filename));
       
should the rest be in my read() method?
       String s;
        while ((s = in.readLine()) != null)
        { // Do something with s
        }
        in.close();
    } catch(IOException e)
    { // Something went wrong, print error message
    }

thanks.
0
 

Expert Comment

by:bjornh
ID: 1227635
I think that should do if you make sure your read method has access to the in-variable, so you have to define it in your class.

This will only work for your read method because you opened your file for reading.

There are lots of other possibilities. I use the book CoreJava, it's quite good though expensive.

Good luck, Bjorn
0

Featured Post

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

Question has a verified solution.

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

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 …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

627 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