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 7013009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:19:50+00:00 2026-05-27T22:19:50+00:00

I wrote some code to read in a text file and to return an

  • 0

I wrote some code to read in a text file and to return an array with each line stored in an element. I can’t for the life of me work out why this isn’t working…can anyone have a quick look? The output from the System.out.println(line); is null so I’m guessing there’s a problem reading the line in, but I can’t see why. Btw, the file i’m passing to it definitely has something in it!

public InOutSys(String filename) {
    try {
        file = new File(filename);
        br = new BufferedReader(new FileReader(file));
        bw = new BufferedWriter(new FileWriter(file));
    } catch (Exception e) {
        e.printStackTrace();
    }
}



public String[] readFile() { 

    ArrayList<String> dataList = new ArrayList<String>();   // use ArrayList because it can expand automatically
    try {
        String line;

        // Read in lines of the document until you read a null line
        do {
            line = br.readLine();
            System.out.println(line);
            dataList.add(line);
        } while (line != null && !line.isEmpty());
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    //  Convert the ArrayList into an Array
    String[] dataArr = new String[dataList.size()];
    dataArr = dataList.toArray(dataArr);

    // Test
    for (String s : dataArr)
        System.out.println(s);

    return dataArr; // Returns an array containing the separate lines of the
    // file
}
  • 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-27T22:19:51+00:00Added an answer on May 27, 2026 at 10:19 pm

    First, you open a FileWriter once after opening a FileReader using new FileWriter(file), which open a file in create mode. So it will be an empty file after you run your program.

    Second, is there an empty line in your file? if so, !line.isEmpty() will terminate your do-while-loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code to read a text file from C drive directly given
I wrote some code to read a file in my Java Servlet class. (I'm
I've already got some code to read a text file using fscanf() , and
I have some code to download a text file from a website. When the
(VB.NET, .NET 3.5) I wrote the following function to read some text from txt
I'm trying to read a text file line-by line. I added a condition where
I wrote some code to tackle a work-related problem. The idea is that the
I wrote some code with a lot of recursion, that takes quite a bit
I wrote some code recently (ISO/ANSI C), and was surprised at the poor performance
I just wrote some code to test the behavior of std::equal, and came away

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.