Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:18:55+00:00 2026-05-27T19:18:55+00:00

I have a text file and a class (Reader) that reads the text file

  • 0

I have a text file and a class (Reader) that reads the text file and stores each line in a String [].

String name;
String [] lines;

Reader(String name){
    this.name = name;
}

public String toString(){
    return this.name;
}

public readFile(String filename){
    String line = "";
    int i = 0;
    try{
        BufferedReader reader = new BufferedReader(new FileReader(filename));

        while(line = reader.readLine()) != null){
            lines[i] = line;
            i++;
        }// while
        reader.close();
    }
    catch(etc...){}
}

I wish to print each array element in table on my jsp page.

Reader r = new Reader("test");
out.print(r.toString());

works and prints ‘test’ but…

r.readFile("test.txt")
for (int i=0; i < r.lines.length; i++)
    out.print(r.lines[i])

does not… However if I run this on the command line its prints the lines [ ] fine

How do I go about doing it in web context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:18:56+00:00Added an answer on May 27, 2026 at 7:18 pm

    Try replacing the following:

    BufferedReader reader = new BufferedReader(new FileReader(filename));
    

    with

    BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(filename)));
    

    That should work since the input file is in the same package as Reader.

    Update:

    I think the problem lies in the TeamData.

    readFile("skytest\\data.file")
    

    That’s not a valid path to the file. Neither in filesystem, nor in the classpath.
    Since, the data.file is in the classpath, you can use getResourceAsStream to load it.
    And, since skytest is the root directory (package), "/skytest/data.file" would also be valid here (the leading / means relative package root). Or, since the file lies in the same package as the TeamData, just the file name should be enough "data.file".

    So, use of the following:

    readFile("data.file") 
    

    And change the following:

    BufferedReader reader = new BufferedReader(new FileReader(requiredFile));
    

    to

    BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(requiredFile)));
    

    Also, the following is really a bad practice (that’s called swallowing the exception):

    catch (IOException ioe) {
        //do something about the exception here
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv reader class that reads a .csv file and its values....
I have text file(test.data), which include some values and class name, for example 4.5,3.5,U1
I have text file with a row like this: SendersTimeSeriesIdentification: COMPANY_A/COMPANY_B/REF_7/20090505 I'd like to
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have Text file that contains data separated with a comma , . How
I have text file with some text information and i need to split this
I have a text file on my local machine that is generated by a
I have a text file of this format: L O A D C A
I have a PHP program that encrypts a PDF file into .xxx file this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.