Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to convert a File object to a String object in java? [duplicate]

Tags:

java

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.

like image 885
gravetii Avatar asked Sep 24 '12 06:09

gravetii


4 Answers

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).

like image 51
rlegendi Avatar answered Nov 01 '22 03:11

rlegendi


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");
like image 31
gigadot Avatar answered Nov 01 '22 02:11

gigadot


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);
}
like image 13
Sean Patrick Floyd Avatar answered Nov 01 '22 03:11

Sean Patrick Floyd


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();
            }
        }
    }
}
like image 4
swemon Avatar answered Nov 01 '22 04:11

swemon