12

Possible Duplicate:
How to create a Java String from the contents of a file

I have a html file which I want to use to extract information. For that I am using Jsoup. Now for using Jsoup, I need to convert the html file into a string. How can I do that?

File myhtml = new File("D:\\path\\report.html")';

Now, I want a String object that contains the content inside the html file.

Community
  • 1
  • 1
gravetii
  • 9,273
  • 9
  • 56
  • 75

7 Answers7

31

I use apache common IO to read a text file into a single string

String str = FileUtils.readFileToString(file);

simple and "clean". you can even set encoding of the text file with no hassle.

String str = FileUtils.readFileToString(file, "UTF-8");
gigadot
  • 8,879
  • 7
  • 35
  • 51
13

Use a library like Guava or Commons / IO. They have oneliner methods.

Guava:

Files.toString(file, charset);

Commons / IO:

FileUtils.readFileToString(file, charset);

Without such a library, I'd write a helper method, something like this:

public String readFile(File file, Charset charset) throws IOException {
    return new String(Files.readAllBytes(file.toPath()), charset);
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
8

With Java 7, it's as simple as:

final String EoL = System.getProperty("line.separator");
List<String> lines = Files.readAllLines(Paths.get(fileName),
        Charset.defaultCharset());

StringBuilder sb = new StringBuilder();
for (String line : lines) {
    sb.append(line).append(EoL);
}
final String content = sb.toString();

However, it does havea few minor caveats (like handling files that does not fit into the memory).

I would suggest taking a look on corresponding section in the official Java tutorial (that's also the case if you have a prior Java).

As others pointed out, you might find sime 3rd party libraries useful (like Apache commons I/O or Guava).

rlegendi
  • 10,466
  • 3
  • 38
  • 50
4

Readin file with file inputstream and append file content to string.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class CopyOffileInputStream {

    public static void main(String[] args) {

        //File file = new File("./store/robots.txt");
        File file = new File("swingloggingsscce.log");

        FileInputStream fis = null;
        String str = "";

        try {
            fis = new FileInputStream(file);
            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                str += (char) content;
            }

            System.out.println("After reading file");
            System.out.println(str);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
swemon
  • 5,840
  • 4
  • 32
  • 54
0

You can copy all contents of myhtml to String as follows:

Scanner myScanner = null;
try
{
    myScanner = new Scanner(myhtml);
    String contents = myScanner.useDelimiter("\\Z").next(); 
}
finally
{
    if(myScanner != null)
    {
        myScanner.close(); 
    }
}

Ofcourse, you can add a catch block to handle exceptions properly.

Azodious
  • 13,752
  • 1
  • 36
  • 71
0

By the way, Jsoup has method that takes file: http://jsoup.org/apidocs/org/jsoup/Jsoup.html#parse(java.io.File,%20java.lang.String)

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
-1

Why you just not read the File line by line and add it to a StringBuffer?

After you reach end of File you can get the String from the StringBuffer.

Stefan
  • 2,603
  • 2
  • 33
  • 62