Solved

open a file

Posted on 1998-07-22
3
191 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now