Solved

open a file

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
throw exception 21 58
Selenium docs api java index 3 48
Setup GlassFish 4 21
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 …
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 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