Solved

open a file

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

705 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

21 Experts available now in Live!

Get 1:1 Help Now