Java:如何在java中将File对象转换为String对象? [英] Java: How to convert a File object to a String object in java?

查看:1365
本文介绍了Java:如何在java中将File对象转换为String对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何根据文件内容创建Java字符串

我有一个html文件,我想用它来提取信息。为此,我正在使用Jsoup。
现在使用Jsoup,我需要将html文件转换为字符串。我该怎么做?

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")';

现在,我想要一个包含html文件内容的String对象。

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

推荐答案

我使用 apache common IO 将文本文件读入单个字符串

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");

这篇关于Java:如何在java中将File对象转换为String对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆