24

How can we read data from a text file and store in a String variable?

is it possible to pass the filename in a method and it would return the String which is the text from the file.

What kind of utilities do I have to import? A list of statements will be great.

6
  • Java, simple java language. Commented Apr 16, 2013 at 1:21
  • why you dont tag it? so let people see Commented Apr 16, 2013 at 1:23
  • You will need java.io.* Commented Apr 16, 2013 at 1:27
  • 4
    The answer to the questions "yes". Take a look at Basic I/O for more details Commented Apr 16, 2013 at 1:28
  • 8
    @MrD - Correction: he's asked people to do his work for him and I asked him what code he has tried. The answer is "he hasn't tried anything" (including google). Feel free to answer this question if you want, but I wouldn't bother. This question will just litter the site. Your answer will just encourage the OP to continue to be lazy and abuse StackOverflow. Commented Apr 16, 2013 at 1:34

2 Answers 2

75

These are the necersary imports:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

And this is a method that will allow you to read from a File by passing it the filename as a parameter like this: readFile("yourFile.txt");

String readFile(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        return sb.toString();
    } finally {
        br.close();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

You could also use Apache's FileUtils library to do this in one line... There are SO many questions about this it's not even funny.
This code has several issues: How do you know the file is the platform default encoding? How do you know that encoding uses a single byte per character? Won't the FileReader need to be closed if reading throws an exception?
Here's a better question/answer that has been around for awhile.
There are more issues. How do you know the file size fits in an int? How do you know the file fits into memory? And finally, and the biggest issue of all, how do you know that reader.read(chars) filled the buffer?
needed to insert this as the while statement to prevent null --> while ((line = br.readLine()) != null)
|
-5

How can we read data from a text file and store in a String Variable?

Err, read data from the file and store it in a String variable. It's just code. Not a real question so far.

Is it possible to pass the filename in a method and it would return the String which is the text from the file.

Yes it's possible. It's also a very bad idea. You should deal with the file a part at a time, for example a line at a time. Reading the entire file into memory before you process any of it adds latency; wastes memory; and assumes that the entire file will fit into memory. One day it won't. You don't want to do it this way.

1 Comment

That would be great if u add some example, it would really help for beginner

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.