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.

asked Sep 24, 2012 at 6:31
3
  • 3
    Check this: stackoverflow.com/questions/326390/… Commented Sep 24, 2012 at 6:33
  • Read the file character by character and put each character in a StringBuffer. When done ask the StringBuffer for the string. Commented Sep 24, 2012 at 6:34
  • 2
    Check jsoup api. It has parse method that takes file. You don't need to read file content manually. Commented Sep 24, 2012 at 6:41

7 Answers 7

32

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");
answered Sep 24, 2012 at 6:35
Sign up to request clarification or add additional context in comments.

3 Comments

How would you do the opposite... readStringToFile
Attached link is expired, can you please post latest URL?
FileUtils.readFileToString is now deprecated with Java 8. It is no longer recommended to use this way.
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);
}
answered Sep 24, 2012 at 6:37

2 Comments

Files.toString is @Deprecated, so the modern way in Guava call Files.asCharSource(file, charset).read().
@DmytroChasovskyi yep. impossible to keep up with Guava's deprecation cycle, thanks
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).

answered Sep 24, 2012 at 6:35

Comments

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();
 }
 }
 }
}
answered Sep 24, 2012 at 6:47

Comments

0
answered Sep 24, 2012 at 6:36

Comments

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.

answered Sep 24, 2012 at 6:36

Comments

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

answered Sep 24, 2012 at 6:35

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.