Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

open a file

Posted on 1998-07-22
3
Medium Priority
?
234 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
  • 2
3 Comments
 

Accepted Solution

by:
bjornh earned 150 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

782 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